我正在处理这样一个表:唯一ID可能有一个条目,也可能有很多条目。
ID | 开始 | 停止天 |
---|---|---|
101 | 2021年1月1日 | 270|
101 | 2020年1月2日 | <2020年3月31日>270 |
102 | 2021年1月 | 2020年3月29日 | 208
102 | 2020年3月30日 | 20203/31 | 208
103 | 2020年1月1日 | 22020年3月31日 | 210
104 | 2020年1月1日 | 2020 | <130>
104 | 2020年1月3日 | 130 |
105 | 2020年1月1日 | 2020年3月31日160 |
你可以试试这个:
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)
打印:
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