[英]Combine two columns of text 'with changing line' in dataframe in pandas/python
Combine two columns of text in dataframe in pandas/python 在pandas / python中的数据框中合并两列文本
So I am trying to change a line with the solution given at the answer above. 因此,我尝试更改上面答案中给出的解决方案。
For example, 例如,
Year quarter period
0 2014 q1 2014q1
1 2015 q2 2015q2
what I'd like to see in my 'period' column is not 2014q1 我想在“期间”栏中看到的不是2014q1
but 但
2014
q1
q1 at the bottm of 2014 2014年第一季度
I wish I could put the content of one column below the content of the different column when two contents of different columns are combined. 我希望可以将两个不同列的内容组合在一起时,将一列的内容放在另一个列的内容之下。
I have tried 我努力了
dataframe["period"] = dataframe["Year"].map(str) +'\n' + dataframe["quarter"]
but it doesn't work. 但这不起作用。 The code above gives me
上面的代码给了我
2014 \n q1
Any advice? 有什么建议吗?
Use DataFrame.stack() : 使用DataFrame.stack() :
df = pd.DataFrame({'Year': [2014,2015], 'period': ['q1','q2']})
df.stack()
It gives you: 它为您提供:
0 Year 2014
period q1
1 Year 2015
period q2
The first (index) column does not explicitly print 0
in the second row and 1
in the last row, but those are implied. 第一(索引)列未在第二行中显式输出
0
,在最后一行中显式打印1
,但暗含了这些。 It has the same meaning as: 它的含义与:
0 Year 2014
0 period q1
1 Year 2015
1 period q2
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.