I want to populate a DataFrame with a date that the Dataframe was ran.
I can populate that data like so:
initial_test_data = pd.DataFrame(dfv, columns = [
'result_video_views',
'result_video_likes',
'result_video_comments'
])
initial_test_data[['date']] = [pd.to_datetime('today').strftime("%m/%d/%Y")]
I get:
result_video_views result_video_likes result_video_comments date
0 32103 2928 367 12/24/2022
My issue is that I want the date to be in the first column. I understand you can rearrange the columns but I would like to know how how to do the reverse.
Whenever I try and execute:
time = pd.to_datetime('today').strftime("%m/%d/%Y")
time[['result_video_views', etc..] = [initial_test_data]
The reverse of the above, I get "TypeError: 'str' object does not support item assignment."
I want to know how to add lists of data as columns into established DataFrames. In order to get around the type error, is this going to need a loop? I've got over 2,000 lines of data and I'm worried if I start adding too many loops I'm going to bog the program down.
CodePudding user response:
Having created the data column using:
initial_test_data[['date']] = [pd.to_datetime('today').strftime("%m/%d/%Y")]
You can change the order to put date first using:
initial_test_data = initial_test_data.reindex( columns = ['date','result_video_views', 'result_video_likes', 'result_video_comments'])
What do you mean by 'the reverse
CodePudding user response:
you can use insert()
and you fixe position of date
initial_test_data.insert(0,'date',pd.to_datetime('today').strftime("%m/%d/%Y"))