1

Let's say I have a dataframe such as

df = pd.DataFrame({'a': [1,2,3], 'b': [['this', 'is', 'a', 'sentence'],['we', 'like', 'pizza'],['hello', 'world']]})

And I want to iterate through the lists in column b and do something like capitalize every letter. I can do something like

for row in df['b']:
    row = [i.upper() for i in row]
    print(row)
# ['THIS', 'IS', 'A', 'SENTENCE']
# ['WE', 'LIKE', 'PIZZA']
# ['HELLO', 'WORLD']

but it doesn't replace the lists in that column. I thought I was reassigning the row values in the for loop but clearly I am not when I print the dataframe after this:

print(df)
#    a                        b
# 0  1  [this, is, a, sentence]
# 1  2        [we, like, pizza]
# 2  3           [hello, world]

What is the proper way to do this? Thanks!

Azathoth
  • 17
  • 1
  • 5
  • 1
    By assigning back to the iterable you're not updating the dataframe, see [here](https://stackoverflow.com/a/48951427/9698684). Do something like `df['b'] = [[s.upper() for s in l] for l in df.b]` – yatu Apr 04 '20 at 22:16

2 Answers2

1

you can use pandas.DataFrame.apply:

 df['b'] =  df['b'].apply(lambda x: list(map(str.upper, x)))
 df

output:

enter image description here

kederrac
  • 16,819
  • 6
  • 32
  • 55
1

I don't know if it is a proper way of doing this, but df.b = df.b.apply(lambda x: [i.upper() for i in x]) should work.