Patrick Nijman
Patrick Nijman

Reputation: 29

appending to lists in column of dataframe

I have a column in a dataframe containing lists of strings as such:

id    colname
1     ['str1', 'str2', 'str3']
2     ['str3', 'str4']
3     ['str3']
4     ['str2', 'str5', 'str6']
..

The strings in the list have some overlap as you can see. I would like to append the lists in this column with a value, for example 'strX'. The end result should be:

id    colname
1     ['str1', 'str2', 'str3', 'strX']
2     ['str3', 'str4', 'strX']
3     ['str3', 'strX']
4     ['str2', 'str5', 'str6', 'strX']
..

What would be the proper way to achieve this? I have tried appending and adding, but these don't get me the desired result.

Upvotes: 0

Views: 25

Answers (2)

RomainL.
RomainL.

Reputation: 1014

So if you want to append "strx" to all you can do as @jezrael point it out like this:

df = pd.DataFrame({"1": [[1,2,3], [3,4,5,6]]}, index=[1,2])
print(df)
              1
1     [1, 2, 3]
2  [3, 4, 5, 6]

df.apply(lambda x: x["1"].append('strx'), axis=1)
print(df)
                    1
1     [1, 2, 3, strx]
2  [3, 4, 5, 6, strx]

But if you want to add a different value based on index you also could! Let take the same df but with a dict that precise what to add

dico = {1: "strx", 2: "strj"}
df.apply(lambda x: x["1"].append(dico[x.name]), axis=1)
print(df)
                    1
1     [1, 2, 3, strx]
2  [3, 4, 5, 6, strj]

Upvotes: 1

jezrael
jezrael

Reputation: 862406

You can add list in Series.map or Series.apply:

df['colname'] = df['colname'].map(lambda x: x + ['strX'])
#alternative
#df['colname'] = df['colname'].apply(lambda x: x + ['strX'])
#or in list comprehension
#df['colname'] = [x + ['strX']  for x in df['colname']]
print (df)
   id                   colname
0   1  [str1, str2, str3, strX]
1   2        [str3, str4, strX]
2   3              [str3, strX]
3   4  [str2, str5, str6, strX]

Upvotes: 0

Related Questions