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

Adding array to Pandas dataframe as row

I want to add an array to an existing pandas dataframe as row. Below is my code :

import pandas as pd
import numpy as np

data = [['tom', 10]]
df = pd.DataFrame(data, columns = ['Name', 'Age'])
print(df)

Y = [10, 100]

df.loc[0] = list(Y)
print(df)

Basically I want to add Y to df as row without disturbing existing rows. I also want to add column names of final df as 'Y1' and 'Y2'

Clearly with above code existing information of df appears to be replaced with Y.

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

Could you please help me with right code?

>Solution :

Use loc adding the value by exiting row and additional columns

df.loc[0,['Y1','Y2']] =Y
df
  Name  Age    Y1     Y2
0  tom   10  10.0  100.0
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