合并数据框行以填充丢失的数据

时间:2019-09-20 08:17:55

标签: python pandas

假设我有一个数据框,其中的行包含丢失的数据,但是有一组列用作键:

import pandas as pd
import numpy as np
data = {"id": [1, 1, 2, 2, 3, 3, 4 ,4], "name": ["John", "John", "Paul", "Paul", "Ringo", "Ringo", "George", "George"], "height": [178, np.nan, 182, np.nan, 175, np.nan, 188, np.nan], "weight": [np.nan, np.NaN, np.nan, 72, np.nan, 68, np.nan, 70]}

df = pd.DataFrame.from_dict(data)
print(df)


id    name  height  weight
0   1    John   178.0     NaN
1   1    John     NaN     NaN
2   2    Paul   182.0     NaN
3   2    Paul     NaN    72.0
4   3   Ringo   175.0     NaN
5   3   Ringo     NaN    68.0
6   4  George   188.0     NaN
7   4  George     NaN    70.0

我将如何使用重复的键向下“挤压”这些行以选择非nan值(如果存在)?

desired output:
id    name  height  weight
0   1    John   178.0     NaN
2   2    Paul   182.0     72.0
4   3   Ringo   175.0     68.0
6   4  George   188.0     70.0

索引无关紧要,并且非NaN数据始终最多只有一行。我想我需要使用groupby(['id', 'name']),但是我不确定从那里去哪里。

1 个答案:

答案 0 :(得分:2)

如果每个组始终只有一个非NaN的值,则可以通过多种方式进行汇总:

df = df.groupby(['id', 'name'], as_index=False).first()

或者:

df = df.groupby(['id', 'name'], as_index=False).last()

或者:

df = df.groupby(['id', 'name'], as_index=False).mean()

或者:

df = df.groupby(['id', 'name'], as_index=False).sum(min_count=1)