Home > Net >  Columns names of a dataframe from a list of names
Columns names of a dataframe from a list of names

Time:02-26

I have a list of names:

list_names = ['Albert','Marcos','Alberta']

and I have an empty dataframe:

t=pd.DataFrame()

How can I add list_names as columns in the dataframe t with values = 10, like this:

Albert  Marcos   Alberta
 10      10        10

CodePudding user response:

t = pd.DataFrame({'Albert': 10, 'Marcos': 10, 'Alberta': 10})

CodePudding user response:

Use reindex to create columns and loc to append data

t =t.reindex(list_names, axis='columns')
t.loc[0]=10
  • Related