简体   繁体   English

在同一个 df 中合并具有相同索引的 Pandas df 行

[英]Combine rows of pandas df with the same index in the same df

If I have a df that looks something like this:如果我有一个看起来像这样的 df:

               v1     v2         ...   v10       v11
id                               ...                            
102717.0   101234650  2018-08-27  ...   NaN       NaN
102717.0   101234650  2018-08-27  ...  UDMS    27/08/2018
102717.0   101234650  2018-08-27  ...   NaN       NaN
102717.0   101234650  2018-08-27  ...  UDMS    27/08/2018

So when the id col matches how could I combine these to just 1 row?因此,当 id col 匹配时,我如何将它们组合为 1 行?

Desired output would be something like:所需的输出类似于:

               v1     v2         ...   v10       v11
id                               ...                            
102717.0   101234650  2018-08-27  ...  UDMS    27/08/2018

So the script would check for all values across each row that are repeated and then reduce it down filling any NaN values...因此,脚本将检查每行中重复的所有值,然后将其减少填充任何 NaN 值...

It really depends on what your results should look like.这实际上取决于您的结果应该是什么样子。 Eg does v2 always contain the same date for the corresponding id?例如,v2 是否总是包含相应 ID 的相同日期? From what I guess you're trying to do I'd do the following:从我猜你正在尝试做的事情来看,我会做以下事情:

mean_dict = dict((el, np.nanmean) for el in df.columns)
newdf = df.groupby('id').agg(mean_dict)

I hope that helps.我希望这有帮助。 With more detailed information of your input and desired output we might be able to help you better.有了有关您的输入和所需输出的更多详细信息,我们或许可以更好地为您提供帮助。

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

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