我有一个数据和一个系列。两者都有相同的指数。我希望在dataframe中将序列值与相同的索引条目一起添加为一行。
我的数据框架和系列如下:
enter code here
index test num %change
cos 7% 20_DA 22 37.93
cos 7% 5_MS 8 13.79
fla 5% 60_DA 67 31.75
fla 7% 20_DA 35 16.59
fla 7% 5_MS 19 9.00
gol 7% 20_DA 22 20.95
gol 7% 5_MS 10 9.52
ill 7% 20_DA 50 64.94
ill 7% 5_MS 29 37.66
lag 7% 20_DA 21 46.67
lag 7% 5_MS 7 15.56
lag 5% 60_DA 18 28.12
lag 7% 20_DA 14 21.88
lag 7% 5_MS 6 9.38
le 5% 60_DA 66 24.81
le 7% 20_DA 67 25.19
le 7% 5_MS 34 12.78
li 7% 20_DA 59 70.24
li 7% 5_MS 23 27.38
po 5% 60_DA 32 12.65
po 7% 20_DA 38 15.02
po 7% 5_MS 23 9.09而系列是
index %change
cos 62.07
fla 68.25
gol 79.05
ill 35.06
lag 53.33
le 75.19
li 29.76
po 87.35我的输出应该像
index test num %change
cos 7% 20_DA 22 37.93
cos 7% 5_MS 8 13.79
cos Nan Nan 62.07请给我一个建议。让我知道,如果标题没有表明相同的意图,数据所做的。
发布于 2018-02-02 16:41:02
假设您的系列名为series,而您的数据访问名为df,则解决方案如下:
pd.merge(df, pd.DataFrame(series), left_index=True, right_index=True)编辑:
在更仔细地查看所需的输出时,您可能需要更多的如下内容:
new_df = pd.concat((data, series), axis=0)
new_df['%change'][len(df):] = new_df[new_df.columns.values[-1]][len(df):]
new_df.drop(new_df.columns.values[-1], axis=1, inplace=True)这将为您提供作为新行添加到'%change'列的系列的值。
https://stackoverflow.com/questions/48587076
复制相似问题