I have a dataframe in which there are 3 columns.One for years, one for months and one for days. What i want is to make ONE column from these 3, in form (years-months-days) so that i can use it to create timeseries as next step. How can I do it?
Asked
Active
Viewed 38 times
0
-
Possible duplicate of [Combine two columns of text in dataframe in pandas/python](https://stackoverflow.com/questions/19377969/combine-two-columns-of-text-in-dataframe-in-pandas-python) – busybear May 18 '19 at 21:54
-
See my answer to this one: https://stackoverflow.com/questions/56203077/how-to-convert-lists-of-years-months-days-into-list-of-datetimes (same problem but just for fun I reposted it below) – qmeeus May 18 '19 at 21:56
1 Answers
0
Assuming that your df looks like this:
df = pd.DataFrame({'Year': ['2014', '2015'], 'Month': ['01', '03'], 'Day': ['03', '16']})
Try that:
df['full-date'] = pd.to_datetime(df[['Year', 'Month', 'Day']].apply(lambda x: '-'.join(x), axis=1))

pmarcol
- 453
- 2
- 9
-
Thanks a lot! It worked! But now i try to re-open my dataframe as timeseries based on the new column that i created and it doesn't open.! I write --> new_df= pd.read_excel(filename, index_col ='full-date',parse_dates = True) and it gives me ----> raise ValueError('Index %s invalid' % col) ValueError: Index full-date invalid – k.vil May 19 '19 at 00:02
-
That's yet another question I guess, but I would try to do it step by step, i.e. read the df first, then convert the `full-date` column to datetime and then set it as index with `set_index` method. – pmarcol May 19 '19 at 19:04