[英]Replace one column of a dataframe's values with another
I am working with a table like this: unique IDs might have 1 entry or they might have many entries.我正在使用这样的表:唯一 ID 可能有 1 个条目,或者它们可能有很多条目。
ID ![]() |
Start![]() |
Stop![]() |
Days![]() |
---|---|---|---|
101 ![]() |
1/1/2021 ![]() |
1/31/2021 ![]() |
270 ![]() |
101 ![]() |
2/1/2020 ![]() |
3/31/2020 ![]() |
270 ![]() |
102 ![]() |
1/1/2021 ![]() |
3/29/2020 ![]() |
208 ![]() |
102 ![]() |
3/30/2020 ![]() |
3/31/2020 ![]() |
208 ![]() |
103 ![]() |
1/1/2020 ![]() |
3/31/2020 ![]() |
210 ![]() |
104 ![]() |
1/1/2020 ![]() |
1/2/2020 ![]() |
130 ![]() |
104 ![]() |
1/3/2020 ![]() |
3/31/2020 ![]() |
130 ![]() |
105 ![]() |
1/1/2020 ![]() |
3/31/2020 ![]() |
160 ![]() |
I need to replace the number in the days column based off another dataframe, which gives a new Days number for each ID.我需要根据另一个 dataframe 替换天数列中的数字,它为每个 ID 提供了一个新的天数。 This dataframe looks like:
这个 dataframe 看起来像:
ID ![]() |
Days![]() |
---|---|
101 ![]() |
290 ![]() |
102 ![]() |
250 ![]() |
103 ![]() |
215 ![]() |
104 ![]() |
180 ![]() |
105 ![]() |
175 ![]() |
How can I replace the Days column in the first dataframe with the Days information for each ID in the second dataframe?如何将第一个 dataframe 中的天数列替换为第二个 dataframe 中每个 ID 的天数信息? My end result should look thus:
我的最终结果应该是这样的:
ID ![]() |
Start![]() |
Stop![]() |
Days![]() |
---|---|---|---|
101 ![]() |
1/1/2021 ![]() |
1/31/2021 ![]() |
290 ![]() |
101 ![]() |
2/1/2020 ![]() |
3/31/2020 ![]() |
290 ![]() |
102 ![]() |
1/1/2021 ![]() |
3/29/2020 ![]() |
250 ![]() |
102 ![]() |
3/30/2020 ![]() |
3/31/2020 ![]() |
250 ![]() |
103 ![]() |
1/1/2020 ![]() |
3/31/2020 ![]() |
215 ![]() |
104 ![]() |
1/1/2020 ![]() |
1/2/2020 ![]() |
180 ![]() |
104 ![]() |
1/3/2020 ![]() |
3/31/2020 ![]() |
180 ![]() |
105 ![]() |
1/1/2020 ![]() |
3/31/2020 ![]() |
175 ![]() |
I tried df.update and converting the second dataframe to a dictionary and using.map but with no success.我尝试了 df.update 并将第二个 dataframe 转换为字典并 using.map 但没有成功。 Any help would be greatly appreciated!
任何帮助将不胜感激!
Thanks,谢谢,
Sean肖恩
You could try this:你可以试试这个:
df_1.drop(columns=Days, inplace=True)
final = pd.merge(df1, df2, on='ID')
final.head()
df_1["Days"] = pd.merge(df_1, df_2, on="ID")["Days_y"]
print(df_1)
Prints:印刷:
ID Start Stop Days
0 101 1/1/2021 1/31/2021 290
1 101 2/1/2020 3/31/2020 290
2 102 1/1/2021 3/29/2020 250
3 102 3/30/2020 3/31/2020 250
4 103 1/1/2020 3/31/2020 215
5 104 1/1/2020 1/2/2020 180
6 104 1/3/2020 3/31/2020 180
7 105 1/1/2020 3/31/2020 175
You can also map()
the first df's ID
with the second df's Days
:您还可以将第一个 df 的
ID
与第二个 df 的Days
map()
:
df1.Days = df1.ID.map(df2.set_index('ID').Days)
# Start Stop Days
# ID
# 101 1/1/2021 1/31/2021 290
# 101 2/1/2020 3/31/2020 290
# 102 1/1/2021 3/29/2020 250
# 102 3/30/2020 3/31/2020 250
# 103 1/1/2020 3/31/2020 215
# 104 1/1/2020 1/2/2020 180
# 104 1/3/2020 3/31/2020 180
# 105 1/1/2020 3/31/2020 175
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.