Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Datetime + String from a Pandas table into a new table. One line of code

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

>Solution :

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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading