How to convert Dataframe into Series?

二次信任 提交于 2019-12-01 07:33:20

You can also use Series class and .values attribute:

pd.Series(df.values.T.flatten())

Output:

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

you need np.flatten

pd.Series(df.values.flatten(order='F'))

out[]
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

You can use unstack

pd.Series(df.unstack().values)

Here's yet another short one.

>>> pd.Series(df.values.ravel(order='F'))                                                                                                               
>>> 
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

Use pd.melt() -

df.melt()['value']

Output

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
Name: value, dtype: int64
df.T.stack().reset_index(drop=True)

Out:

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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!