我有一只熊猫系列S:
Date
2/27/2017 149
2/28/2017 150
3/01/2017 154
3/04/2017 152
3/12/2017 156
3/17/2017 148
我还有一个Dataframe df
Date A B C PS
2/28/2017 12:42:05 1 2 4 2/27/2017
2/28/2017 12:42:07 1 2 4 2/27/2017
2/28/2017 12:42:08 1 2 4 2/27/2017
2/28/2017 12:42:55 1 2 4 2/27/2017
3/01/2017 12:42:05 1 2 4 2/28/2017
3/01/2017 12:42:07 1 2 4 2/28/2017
3/01/2017 12:42:08 1 2 4 2/28/2017
3/01/2017 12:42:09 1 2 4 2/28/2017
3/05/2017 12:42:05 1 2 4 3/04/2017
3/05/2017 12:42:07 1 2 4 3/04/2017
3/05/2017 12:42:08 1 2 4 3/04/2017
3/05/2017 12:42:09 1 2 4 3/04/2017
A,B,C确实有所改变,但它们与此问题无关。
我希望输出数据框如下:
Date A B C PS Value
2/28/2017 12:42:05 1 2 4 2/27/2017 149
2/28/2017 12:42:07 1 2 4 2/27/2017 149
2/28/2017 12:42:08 1 2 4 2/27/2017 149
2/28/2017 12:42:55 1 2 4 2/27/2017 149
3/01/2017 12:42:05 1 2 4 2/28/2017 150
3/01/2017 12:42:07 1 2 4 2/28/2017 150
3/01/2017 12:42:08 1 2 4 2/28/2017 150
3/01/2017 12:42:09 1 2 4 2/28/2017 150
3/05/2017 12:42:05 1 2 4 3/04/2017 152
3/05/2017 12:42:07 1 2 4 3/04/2017 152
3/05/2017 12:42:08 1 2 4 3/04/2017 152
3/05/2017 12:42:09 1 2 4 3/04/2017 152
基本上我想在df中添加一个名为Value的列,其中Value是与系列S中的Date对应的任何值,即df的PS列。
伪代码将是 df ["值"] = S [df [PS]]
我不想将系列中的日期列/索引结束。
答案 0 :(得分:0)
或者,从系列中创建mapping
并使用df.map
或df.replace
。
首先,创建一个映射:
In [1221]: mapping = dict(zip(s.index, s.values))
替换df.PS
:
In [1222]: df['Value'] = df.PS.replace(mapping)
In [1223]: df
Out[1223]:
A B C PS Value
Date
2/28/2017 12:42:05 1 2 4 2/27/2017 149
2/28/2017 12:42:07 1 2 4 2/27/2017 149
2/28/2017 12:42:08 1 2 4 2/27/2017 149
2/28/2017 12:42:55 1 2 4 2/27/2017 149
3/01/2017 12:42:05 1 2 4 2/28/2017 150
3/01/2017 12:42:07 1 2 4 2/28/2017 150
3/01/2017 12:42:08 1 2 4 2/28/2017 150
3/01/2017 12:42:09 1 2 4 2/28/2017 150
3/05/2017 12:42:05 1 2 4 3/04/2017 152
3/05/2017 12:42:07 1 2 4 3/04/2017 152
3/05/2017 12:42:08 1 2 4 3/04/2017 152
3/05/2017 12:42:09 1 2 4 3/04/2017 152
您还可以使用df.map
:
In [1224]: df['Value'] = df.PS.map(mapping)
In [1225]: df
Out[1225]:
A B C PS Value
Date
2/28/2017 12:42:05 1 2 4 2/27/2017 149
2/28/2017 12:42:07 1 2 4 2/27/2017 149
2/28/2017 12:42:08 1 2 4 2/27/2017 149
2/28/2017 12:42:55 1 2 4 2/27/2017 149
3/01/2017 12:42:05 1 2 4 2/28/2017 150
3/01/2017 12:42:07 1 2 4 2/28/2017 150
3/01/2017 12:42:08 1 2 4 2/28/2017 150
3/01/2017 12:42:09 1 2 4 2/28/2017 150
3/05/2017 12:42:05 1 2 4 3/04/2017 152
3/05/2017 12:42:07 1 2 4 3/04/2017 152
3/05/2017 12:42:08 1 2 4 3/04/2017 152
3/05/2017 12:42:09 1 2 4 3/04/2017 152
答案 1 :(得分:-1)
使用.merge():
将数据框与系列合并new_df = df.merge(pd.DataFrame(s, columns=['Value']), right_index=True, left_on='PS', how='outer')
您还可以迭代PS并从系列中获取值:
df.loc[:, 'Value'] = [s[ps] for ps in df['PS']]