3 回答
TA贡献1802条经验 获得超5个赞
这将删除列名,但完成工作:
import pandas as pd
df = pd.DataFrame([['hey how are you', 'fine thanks'],
['good to know', 'yes, and you'],
['I am fine','ok'],
['see you','bye!']],columns=list('AB'))
df.stack().reset_index(drop=True)
0 hey how are you
1 fine thanks
2 good to know
3 yes, and you
4 I am fine
5 ok
6 see you
7 bye!
dtype: object
默认堆栈行为保留列名:
df.stack()
0 A hey how are you
B fine thanks
1 A good to know
B yes, and you
2 A I am fine
B ok
3 A see you
B bye!
dtype: object
如果您有更多列,您可以选择要堆叠的列,只需使用列索引:
df[["A", "B"]].stack()
使用额外的列,事情变得棘手,您需要通过降低一级(包含列)来对齐索引:
df["C"] = range(4)
stacked = df[["A", "B"]].stack()
stacked.index = stacked.index.droplevel(level=1)
stacked
0 hey how are you
0 fine thanks
1 good to know
1 yes, and you
2 I am fine
2 ok
3 see you
3 bye!
dtype: object
现在我们可以连接C列:
pd.concat([stacked, df["C"]], axis=1)
0 C
0 hey how are you 0
0 fine thanks 0
1 good to know 1
1 yes, and you 1
2 I am fine 2
2 ok 2
3 see you 3
3 bye! 3
TA贡献1796条经验 获得超10个赞
您可能正在寻找的是pandas.concat
.
它接受“Series、DataFrame 或 Panel 对象的序列或映射”,因此您可以传递选择列list
的DataFrame
对象(将pd.Series
在为单个列编制索引时)。
df3 = pd.concat([df['A'], df['B']])
添加回答
举报