简体   繁体   English

如何从Python数据框的多个列中选择所有非NULL值

[英]How to pick out all non-NULL value from multiple columns in Python Dataframe

I had a DataFrame like below: 我有一个DataFrame如下:

       column-a         column-b      column-c
0          Nan             A              B
1           A              Nan            C
2           Nan            Nan            C
3           A              B              C

I hope to create a new column-D to capture all non-NULL values from column A to C: 我希望创建一个新的列D来捕获从列A到C的所有非NULL值:

        column d
0        A,B
1        A,C
2        C
3        A,B,C

Thanks! 谢谢!

You need to change the 'Nan' to np.nan , then using stack with groupby join 您需要更改'Nan'np.nan ,然后使用stackgroupby join

df=df.replace('Nan',np.nan)
df.stack().groupby(level=0).agg(','.join)
Out[570]: 
0      A,B
1      A,C
2        C
3    A,B,C
dtype: object

#df['column-d']= df.stack().groupby(level=0).agg(','.join)

After fixing the nan s: 修复nan后:

df = df.replace('Nan', np.nan)

collect all non-null values in each row in a list and join the list items. 收集列表中每一行的所有非空值,并加入列表项。

df['column-d'] = df.apply(lambda x: ','.join(x[x.notnull()]), axis=1)
#0      A,B
#1      A,C
#2        C
#3    A,B,C

Surprisingly, this solution is somewhat faster than the stack/groupby solution by Wen, at least for the posted dataset. 出乎意料的是,至少对于发布的数据集,此解决方案比Wen的stack/groupby解决方案要快一些。

暂无
暂无

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

相关问题 从熊猫数据框中的多个列创建一个包含所有非空值的单个列 - create a single column containing all non-null values from multiple columns in a pandas dataframe 如何将 pandas dataframe 中的 null 值替换为 Z6A55075B3CDF4754 中同一列中的非空值? - How to replace a null value from a pandas dataframe with a non-null value from the same column in the dataframe? 如何将 Pandas Dataframe 中某些列的非空值填充到新列中? 如何在多个条件下使用 np.where()? - How to fill Non-Null values from some columns in Pandas Dataframe into a new column? How to use np.where() for multiple conditions? 将 dataframe 的多列与非空值的分隔符连接起来 - Concatenate multiple columns of dataframe with a seperating character for Non-null values 如何根据 Python 中的非空列的字典创建 dataframe 列 - How to create dataframe columns based on dictionaries for non-null columns in Python 根据非空列数从数据框中选择行 - Select rows from a dataframe based on the number of non-null columns 从PySpark DataFrame中的非空列中选择值 - Selecting values from non-null columns in a PySpark DataFrame 如何将DataFrame列的非空条目组合到新列中? - How to combine non-null entries of columns of a DataFrame into a new column? 如何根据另一个日期时间列顺序并按 ID 分组从多个列中获取第一个非空值? - How do I get the first non-null value from multiple columns based on another datetime column order and grouped by ID? 从pandas DataFrame返回最后一个有效(非null)值 - Returning the last valid (non-null) value from a pandas DataFrame
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM