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!