[英]concat column values with previous row if there is null in another column in same row
I have a data frame like this,我有一个这样的数据框,
df: df:
col1 col2 col3
1 cat 4
nan dog nan
3 tiger 3
2 lion 9
nan frog nan
nan elephant nan
I want to create a data frame from this data frame that id there is nan values in col1, col2 values will be added to the previous row value.我想从这个数据框创建一个数据框,id 在 col1 中有 nan 值,col2 值将被添加到前一行值。
for example the desired output data frame will be:例如,所需的输出数据帧将是:
col1 col2 col3
1 catdog 4
3 tiger 3
2 lionfrogelephant 9
How to do this using pandas ?如何使用熊猫做到这一点?
Use forward filling missing values and aggregate join
:使用前向填充缺失值和聚合join
:
cols = ['col1','col3']
df[cols] = df[cols].ffill()
df = df.groupby(cols)['col2'].apply(''.join).reset_index()
print (df)
col1 col3 col2
0 1.0 4.0 catdog
1 2.0 9.0 lionfrogelephant
2 3.0 3.0 tiger
Or if necessary forward filling missing values in all columns:或者,如有必要,在所有列中向前填充缺失值:
df = df.ffill().groupby(['col1','col3'])['col2'].apply(''.join).reset_index()
print (df)
col1 col3 col2
0 1.0 4.0 catdog
1 2.0 9.0 lionfrogelephant
2 3.0 3.0 tiger
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.