我缺少记录,我需要通过一列连接从一行到另一行。
以下是一个例子:
df1 = pd.DataFrame({'animal':['horse','cat','cat','dog'],
'type':['big','small',np.nan,'medium'],
'rating':[1,2,np.nan,2]})
print(df1)
输出:
animal rating type
0 horse 1 big
1 cat 2 small
2 cat NaN NaN
3 dog 2 medium
期望的输出:
animal rating type
0 horse 1 big
1 cat 2 small
2 cat 2 small
3 dog 2 medium
答案 0 :(得分:2)
如果你的行并不总是正确排列为ffill(),你可以通过将数据帧分成好的和坏的集合,修复坏集和重新组合来实现相同的结果。例如:
df1 = pd.DataFrame({'animal':['horse','cat','cat','dog'],
'type':['big','small',np.nan,'medium'],
'rating':[1,2,np.nan,2]})
df1.set_index('animal',inplace=True)
good_df1 = df1[~df1.isnull().any(axis=1)]
bad_df1 = df1[df1.isnull().any(axis=1)]
final = pd.concat([good_df1, bad_df1.fillna(good_df1)]).reset_index()
给出了:
animal rating type
0 horse 1.0 big
1 cat 2.0 small
2 dog 2.0 medium
3 cat 2.0 small
要仅填充某些列,请将最后一行替换为:
fill_cols = ['rating']
final = pd.concat([good_df1, bad_df1[fill_cols].fillna(good_df1[fill_cols])]).reset_index()
或者:
fill_cols = list(df1.columns)
fill_cols.remove('type')
final = pd.concat([good_df1, bad_df1[fill_cols].fillna(good_df1[fill_cols])]).reset_index()
或者,如果您的数据集允许您可以使用所提到的ffill(),但需要额外的排序步骤以确保首先获得良好的数据:
df1.sort_values(['animal','rating','type']).fillna(method='ffill')