Home > front end >  Datetime String from a Pandas table into a new table. One line of code
Datetime String from a Pandas table into a new table. One line of code

Time:05-13

I'm a beginner at python. I'm moving specific cells/scalars from one Dataframe to another. I'm trying to work out why my first block of code didn't work but my expanded code does. Why does concat give an error? My Initial Dataframes:

df1_Data

df1_Data

and df2_Data

df2_Data

and code

date_string=df1_Data.iat[0,2]
date_string.strftime("%Y-%m-%d, %H:%M:%S")    
df2_Data.iat[0,0] = pd.concat([date_string,df1_Data.iat[2,2]])

Gives this error:

cannot concatenate object of type '<class 'datetime.datetime'>'; only Series and DataFrame objs are valid

Expanded code

date_string = df1_Data.iat[0,2]
date_string.strftime("%Y-%m-%d, %H:%M:%S")
PersonDate= (df1_Data.iat[2,2],(date_string))
df2_Data.iat[0,0] = PersonDate

Gives my target Dataframe:

df2_Data

Any ideas are welcome.

CodePudding user response:

First idea is transpose rows for one row DataFrame:

df = df1_Data.set_index(df1_Data.columns[0]).iloc[:, 2].T

df.columns = df.columns.str.strip(':')

Your code - you need convert values to strings for possible join together:

date_string=df1_Data.iat[0,2].strftime("%Y-%m-%d, %H:%M:%S")

df2_Data.iat[0,0] = df1_Data.iat[2,2]   ','   str(date_string)
  • Related