简体   繁体   English

Python pandas如何将多行合并为一个?

[英]Python pandas how to merge multiple row into one?

I have A dataframe as below: 我有一个数据帧如下:

Car        SeriesNo     White       Red        Black
Proton     A111           2       
Proton     A111                      4
Proton     A111                                  5

My Expected output will be: 我的预期输出将是:

Car        SeriesNo     White       Red        Black
Proton     A111           2          4           5

Anyone have ideas on this? 有人对此有什么想法吗?

Use GroupBy.first for get first non missing values per groups: 使用GroupBy.first获取每个组的第一个非缺失值:

#if necessary replace empty strings to missing values
#df = df.replace('',np.nan)
df = df.groupby(['Car','SeriesNo'], as_index=False).first()
print (df)
      Car SeriesNo  White  Red  Black
0  Proton     A111    2.0  4.0    5.0

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

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