I have a big dataframe of datetime and values so I will just be taking a sample dataframe called df1
DateTime Values
2020-12-26 14:00:00 439.0
2020-12-26 14:00:00 441.0
2020-12-26 14:00:00 461.0
2020-12-27 08:00:00 536.0
2020-12-27 08:00:00 547.0
2020-12-27 08:00:00 484.0
2020-12-27 08:00:00 497.0
2020-12-27 14:00:00 491.0
2020-12-27 14:00:00 512.0
2020-12-27 14:00:00 529.0
2020-12-27 14:00:00 436.0
and I have another full dataframe df 2
DateTime Trend
2020-12-18 14:00 no trend
2020-12-19 08:00 no trend
2020-12-19 14:00 no trend
2020-12-21 08:00 no trend
2020-12-21 14:00 no trend
2020-12-22 08:00 no trend
2020-12-22 14:00 decreasing
2020-12-23 08:00 no trend
2020-12-23 14:00 no trend
2020-12-24 08:00 no trend
2020-12-24 14:00 no trend
2020-12-25 08:00 no trend
2020-12-25 14:00 decreasing
2020-12-26 08:00 no trend
2020-12-26 14:00 decreasing
2020-12-27 08:00 no trend
2020-12-27 14:00 no trend
I am trying to create a new column df1[Trend] and assign the trend values based on the right date time
The result should be like this
DateTime Values Trend
2020-12-26 14:00:00 439.0 decreasing
2020-12-26 14:00:00 441.0 decreasing
2020-12-26 14:00:00 461.0 decreasing
2020-12-27 08:00:00 536.0 no trend
2020-12-27 08:00:00 547.0 no trend
2020-12-27 08:00:00 484.0 no trend
2020-12-27 08:00:00 497.0 no trend
2020-12-27 14:00:00 491.0 no trend
2020-12-27 14:00:00 512.0 no trend
2020-12-27 14:00:00 529.0 no trend
2020-12-27 14:00:00 436.0 no trend
Is there anyway I could it?
Use pandas.DataFrame.merge
:
df1.merge(df2)
DateTime Values Trend
0 2020-12-26 14:00:00 439.0 decreasing
1 2020-12-26 14:00:00 441.0 decreasing
2 2020-12-26 14:00:00 461.0 decreasing
3 2020-12-27 08:00:00 536.0 no trend
4 2020-12-27 08:00:00 547.0 no trend
5 2020-12-27 08:00:00 484.0 no trend
6 2020-12-27 08:00:00 497.0 no trend
7 2020-12-27 14:00:00 491.0 no trend
8 2020-12-27 14:00:00 512.0 no trend
9 2020-12-27 14:00:00 529.0 no trend
10 2020-12-27 14:00:00 436.0 no trend
EDIT
You're getting
ValueError: You are trying to merge on datetime64[ns] and object columns. If you wish to proceed you should use pd.concat
because df1["DateTime"]
is a datetime column but df2["DateTime"]
is a str
column then you can't merge. To fix it convert df2["DateTime"]
to datetime with pandas.to_datetime
:
df2['DateTime'] = pd.to_datetime(df2.DateTime)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.