简体   繁体   English

如何在熊猫中将n列合并为1行

[英]How to consolidate n columns to 1 row in Pandas

I have a DataFrame containing ~40 columns and over 150K rows that I want to consolidate into just one column. 我有一个包含约40列和超过15万行的DataFrame,我想将其合并为一列。 The DataFrame has NaN values all over the place. DataFrame到处都有NaN值。

Here's an example of what my df looks like: 这是我的df外观的示例:

d = {'A' : pd.Series([np.nan, 5., 3.], index=[0,1,2]),
 'B' : pd.Series([np.nan, 2., np.nan], index=[0,1,2]),
 'C' : pd.Series([1.,np.nan, 4.], index=[0,1,2])}
df = pd.DataFrame(d)

     A    B    C
0  NaN  NaN  1.0
1  5.0  2.0  NaN
2  3.0  NaN  4.0

I want my resulting df to contain all the values from all columns, but only one column. 我希望得到的df包含所有列中的所有值,但仅包含一列。 The rows can have multiple values in multiple columns, so I need a way to keep them all like so: 这些行可以在多个列中具有多个值,因此我需要一种使它们都像这样的方式:

e = {'ABC' : pd.Series([1.,5.,2.,3.,4.], index=[0,1,2,3,4])}
df1 = pd.DataFrame(e)
   ABC
0  1.0
1  5.0
2  2.0
3  3.0
4  4.0

The column names are all different so I haven't been able to join, merge or concatenate them. 列名都是不同的,因此我无法加入,合并或连接它们。

Thanks in advance! 提前致谢!

Maybe stack seems to be a good option which automatically drops all NaNs by default leaving with just finite entries: 也许stack似乎是一个不错的选择,它默认会自动丢弃所有NaNs ,而只留下有限的条目:

pd.DataFrame({'ABC': df.stack().values})

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM