本文介绍了如何将Dataframe转换为Series?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想将N列转换为一个系列.如何有效地做到这一点?
I want to convert N columns into one series. How to do it effectively?
输入:
0 1 2 3
0 64 98 47 58
1 80 94 81 46
2 18 43 79 84
3 57 35 81 31
预期输出:
0 64
1 80
2 18
3 57
4 98
5 94
6 43
7 35
8 47
9 81
10 79
11 81
12 58
13 46
14 84
15 31
dtype: int64
到目前为止,我尝试过:
So Far I tried:
print df[0].append(df[1]).append(df[2]).append(df[3]).reset_index(drop=True)
我对我的解决方案不满意,而且它不适用于动态列.请帮助我找到更好的方法.
I'm not satisfied with my solution, moreover it won't work for dynamic columns. Please help me to find a better approach.
推荐答案
您还可以使用Series
类和.values
属性:
You can also use Series
class and .values
attribute:
pd.Series(df.values.T.flatten())
输出:
0 64
1 80
2 18
3 57
4 98
5 94
6 43
7 35
8 47
9 81
10 79
11 81
12 58
13 46
14 84
15 31
dtype: int64
这篇关于如何将Dataframe转换为Series?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!