简体   繁体   English

用另一列替换数据框值的一列

[英]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 ID Start开始 Stop停止 Days
101 101 1/1/2021 2021 年 1 月 1 日 1/31/2021 2021 年 1 月 31 日 270 270
101 101 2/1/2020 2020 年 2 月 1 日 3/31/2020 2020 年 3 月 31 日 270 270
102 102 1/1/2021 2021 年 1 月 1 日 3/29/2020 2020 年 3 月 29 日 208 208
102 102 3/30/2020 2020 年 3 月 30 日 3/31/2020 2020 年 3 月 31 日 208 208
103 103 1/1/2020 2020 年 1 月 1 日 3/31/2020 2020 年 3 月 31 日 210 210
104 104 1/1/2020 2020 年 1 月 1 日 1/2/2020 2020 年 1 月 2 日 130 130
104 104 1/3/2020 2020 年 1 月 3 日 3/31/2020 2020 年 3 月 31 日 130 130
105 105 1/1/2020 2020 年 1 月 1 日 3/31/2020 2020 年 3 月 31 日 160 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 ID Days
101 101 290 290
102 102 250 250
103 103 215 215
104 104 180 180
105 105 175 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 ID Start开始 Stop停止 Days
101 101 1/1/2021 2021 年 1 月 1 日 1/31/2021 2021 年 1 月 31 日 290 290
101 101 2/1/2020 2020 年 2 月 1 日 3/31/2020 2020 年 3 月 31 日 290 290
102 102 1/1/2021 2021 年 1 月 1 日 3/29/2020 2020 年 3 月 29 日 250 250
102 102 3/30/2020 2020 年 3 月 30 日 3/31/2020 2020 年 3 月 31 日 250 250
103 103 1/1/2020 2020 年 1 月 1 日 3/31/2020 2020 年 3 月 31 日 215 215
104 104 1/1/2020 2020 年 1 月 1 日 1/2/2020 2020 年 1 月 2 日 180 180
104 104 1/3/2020 2020 年 1 月 3 日 3/31/2020 2020 年 3 月 31 日 180 180
105 105 1/1/2020 2020 年 1 月 1 日 3/31/2020 2020 年 3 月 31 日 175 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.

相关问题 用另一个数据帧的值替换一个数据帧中的列值 - replace column values in one dataframe by values of another dataframe 根据另一列的值替换Pandas数据框的Column的值 - Replace values of a Pandas dataframe's Column based on values of another column 用另一列 Pandas DataFrame 替换一列中的值 - Replace values from one column with another column Pandas DataFrame 通过将另一列与第二个DataFrame进行比较,替换一列中的值 - Replace values from one column by comparing another column to a second DataFrame 将 dataframe 的一列中的 nan 值替换为第二个 dataframe 的另一列值 - Replace nan values in one column of a dataframe with another column value of second dataframe 如何在另一个数据帧中查找一个数据帧的 2 列值并在找到匹配时替换初始值? - How to find 2 column values of one dataframe in another dataframe and replace the initial values if match found? Python pandas 用模式(同一列 -A)相对于 Pandas 数据帧中的另一列替换一列(A)的 NaN 值 - Python pandas replace NaN values of one column(A) by mode (of same column -A) with respect to another column in pandas dataframe 比较另一列 dataframe 中一列的值 dataframe - Compare values of one column of dataframe in another dataframe 替换python数据框值并存储在另一列中 - Replace python dataframe values and store in another column Pandas 将列的值替换为与另一个 Dataframe 的比较 - Pandas replace values of a column with comparison to another Dataframe
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM