I had a Dataframe with this kind of date
| Year | Day | Hour | Minute |
|---|---|---|---|
| 2017 | 244 | 0 | 0 |
| 2017 | 244 | 0 | 1 |
| 2017 | 244 | 0 | 2 |
I want to create a new column on this DataFrame showing the date hour minute but I don't know how to convert the days into months and unify everything
I try something using pd.to_datetime like the code below.
line['datetime'] = pd.to_datetime(line['Year'] line['Day'] line['Hour'] line['Minute'], format= '%Y%m%d %H%M')
I would like to have something like this:
| Year | Month | Day | Hour | Minute |
|---|---|---|---|---|
| 2017 | 9 | 1 | 0 | 0 |
| 2017 | 9 | 1 | 0 | 1 |
| 2017 | 9 | 1 | 0 | 2 |
CodePudding user response:
Try:
s = pd.to_datetime(df['Year'], format='%Y') \
pd.TimedeltaIndex(df['Day']-1, unit='D')
print(s)
# Output
0 2017-09-01
1 2017-09-01
2 2017-09-01
dtype: datetime64[ns]
Now you can insert your columns:
df.insert(1, 'Month', s.dt.month)
df['Day'] = s.dt.day
print(df)
# Output
Year Month Day Hour Minute
0 2017 9 1 0 0
1 2017 9 1 0 1
2 2017 9 1 0 2
CodePudding user response:
So in your case do
df['date'] = pd.to_datetime(df.astype(str).agg(' '.join,1),format='%Y %j %H %M')
Out[294]:
0 2017-09-01 00:00:00
1 2017-09-01 00:01:00
2 2017-09-01 00:02:00
dtype: datetime64[ns]
#df['month'] = df['date'].dt.month
#df['day'] = df['date'].dt.day
CodePudding user response:
df["Month"]=round(df["Day"]/30 .5).astype(int)
This establishes a new column and populates taht column by using the day column to calculate the month (total days / 30), rounding up by adding .5 and inserting it as an integer using astype
Example screenshot

