Home > Enterprise >  Add a column with a fix value into a list with multiple lists in Python
Add a column with a fix value into a list with multiple lists in Python

Time:01-07

I have multiple lists inside a main list that looks like following:

print(result_list)

[      rating  \
0   3 stars
1   3 stars   
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               
place
0   L’Osteria
1   Shiva,

rating  \
0   5 stars
1   4 stars
2   3 stars
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               place  
0   Bio Bakery
1   Nova Era Bakery
2   Panino Italiano
]

When typing type(result_list[0]) it will return pandas.core.frame.DataFrame

I want to add a third column named "category". This column should have a fix value assigned such as “Restaurant” or “Bakery”. The output should look like following:

[      rating  \
2   3 stars
3   3 stars   
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               
place
2   L’Osteria
3   Shiva

category
0   Restaurant
1   Restaurant,


rating  \
3   5 stars
4   4 stars
5   3 stars
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               
place  
3   Bio Bakery
4   Nova Era Bakery
5   Panino Italiano

category
0   Bakery
1   Bakery


]

So far I have tried among other things this, but it is not working.

lista = ['Restaurant','Bakery']

for i,e in enumerate(result_list):
    #print('index:',i)
    result_list.append(lista[i])

I am missing the logic here. Please help!


CodePudding user response:

The elements are pandas dataframes. So just add the new column to each df.

for df, category in zip(result_list, lista):
    df['Category'] = category
  • Related