Home > database >  Append values to list within a pandas column
Append values to list within a pandas column

Time:11-12

As simple as this sounds I cannot figure it out... Here's what I want to do

df = pd.DataFrame(
[
{'first names' :  ['trey', 'tagg']},
{'first names' :  ['teague', 'tanner']},
]
)

df

first names
0 [trey, tagg]
1 ['teague', 'tanner']

I would like to do something such as df['first names'].append('joe') and get the result..

first names
0 [trey, tagg, joe]
1 ['teague', 'tanner', joe]

CodePudding user response:

Try:

df["first names"].apply(lambda lst: lst.append("joe"))
print(df)

Prints:

             first names
0      [trey, tagg, joe]
1  [teague, tanner, joe]

CodePudding user response:

You can try

for val in df['first names']:
    val.append('jeo')
print(df)

output

             first names
0      [trey, tagg, jeo]
1  [teague, tanner, jeo]
  • Related