简体   繁体   English

将日期时间索引与日期时间列进行比较并更改另一列中的相应值

[英]Compare datetime index with a datetime column and change the corresponding value in another column

Please consider the following dataframe in CSV format:请考虑以下 CSV 格式的数据框:

time,transaction_hash,index,value,recipient,spending_time,sender
2009-01-09 03:54:39,a0241106ee5a597c9,0,50.0,Paul,2009-01-12 03:30:25,Coinbase
2009-01-12 03:30:25,1338530e9831e9e16,1,40.0,Me,2009-01-12 06:02:13,Unknown
2009-01-12 06:02:13,546c4fb42b41e14be,1,30.0,John,2009-01-12 06:12:16,Unknown
2009-01-12 06:02:14,eaf0775ebca408f7r,1,17.0,Paul,2010-09-22 06:06:02,Unknown
2009-01-12 06:02:15,732a865bf5414eab2,1,15.0,Paul,2010-02-23 14:01:23,Unknown
2009-01-12 06:12:16,591e911da64588073,1,29.0,John,2009-01-12 06:34:22,Unknown
2009-01-12 06:34:22,12b52732a85c191ba,1,28.0,Sara,2009-01-12 20:04:20,Unknown

I wanna compare the rows in the index time with the rows in the column spending_time and if they are equal, substitute the Unknown value in the sender column (associated with index time ) by the value of the recipient (corresponding to the spending_time column), as follows:我想将索引time中的行与spending_time time列中的行进行比较,如果它们相等,则将sender列中的Unknown值(与索引time关联)替换为recipient的值(对应于spending_time列),如下:

time,transaction_hash,index,value,recipient,spending_time,sender
2009-01-09 03:54:39,a0241106ee5a597c9,0,50.0,Paul,2009-01-12 03:30:25,Coinbase
2009-01-12 03:30:25,1338530e9831e9e16,1,40.0,Me,2009-01-12 06:02:13,Paul
2009-01-12 06:02:13,546c4fb42b41e14be,1,30.0,John,2009-01-12 06:12:16,Me
2009-01-12 06:02:14,eaf0775ebca408f7r,1,17.0,Paul,2010-09-22 06:06:02,Unknown
2009-01-12 06:02:15,732a865bf5414eab2,1,15.0,Paul,2010-02-23 14:01:23,Unknown
2009-01-12 06:12:16,591e911da64588073,1,29.0,John,2009-01-12 06:34:22,John
2009-01-12 06:34:22,12b52732a85c191ba,1,28.0,Sara,2009-01-12 20:04:20,John

Merge the dataframe with itself on the two related columns ( time and spending_time ).在两个相关列( timespending_time time )上将数据帧与自身合并。

df = df.merge(df[['spending_time', 'recipient']], left_on='time', right_on='spending_time', how='left', suffixes=('', '_y'))

Intermidiate result:中间结果:

                  time   transaction_hash  index  value recipient        spending_time    sender      spending_time_y recipient_y
0  2009-01-09 03:54:39  a0241106ee5a597c9      0   50.0      Paul  2009-01-12 03:30:25  Coinbase                  NaN         NaN
1  2009-01-12 03:30:25  1338530e9831e9e16      1   40.0        Me  2009-01-12 06:02:13   Unknown  2009-01-12 03:30:25        Paul
2  2009-01-12 06:02:13  546c4fb42b41e14be      1   30.0      John  2009-01-12 06:12:16   Unknown  2009-01-12 06:02:13          Me
3  2009-01-12 06:02:14  eaf0775ebca408f7r      1   17.0      Paul  2010-09-22 06:06:02   Unknown                  NaN         NaN
4  2009-01-12 06:02:15  732a865bf5414eab2      1   15.0      Paul  2010-02-23 14:01:23   Unknown                  NaN         NaN
5  2009-01-12 06:12:16  591e911da64588073      1   29.0      John  2009-01-12 06:34:22   Unknown  2009-01-12 06:12:16        John
6  2009-01-12 06:34:22  12b52732a85c191ba      1   28.0      Sara  2009-01-12 20:04:20   Unknown  2009-01-12 06:34:22        John

The new sender column can now be obtained by merging sender with recipient_y :现在可以通过将senderrecipient_y合并来获得新的sender列:

df['sender'] = df['recipient_y'].combine_first(df['sender'])
df = df.drop(columns=['spending_time_y', 'recipient_y'])

Result:结果:

                  time   transaction_hash  index  value recipient        spending_time    sender
0  2009-01-09 03:54:39  a0241106ee5a597c9      0   50.0      Paul  2009-01-12 03:30:25  Coinbase
1  2009-01-12 03:30:25  1338530e9831e9e16      1   40.0        Me  2009-01-12 06:02:13      Paul
2  2009-01-12 06:02:13  546c4fb42b41e14be      1   30.0      John  2009-01-12 06:12:16        Me
3  2009-01-12 06:02:14  eaf0775ebca408f7r      1   17.0      Paul  2010-09-22 06:06:02   Unknown
4  2009-01-12 06:02:15  732a865bf5414eab2      1   15.0      Paul  2010-02-23 14:01:23   Unknown
5  2009-01-12 06:12:16  591e911da64588073      1   29.0      John  2009-01-12 06:34:22      John
6  2009-01-12 06:34:22  12b52732a85c191ba      1   28.0      Sara  2009-01-12 20:04:20      John

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM