Reputation: 1185
I'm new to pandas and have a column in a pandas dataframe that contains strings (non-lowercase) that I want to convert to lowercase. The dataframe column is called:
df['labels']
and its elements are all lists (of strings):
0 ["Cat", "Dog", "Horse"]
1 ["Pig", "Fish", "Giraffe"]
....
I would like to lower case each string in the list, intuitively I tried this:
for element in input_data['labels']:
for word in element:
word.lower()
but on a print(input_data["labels"]
nothing was lowercased.
Upvotes: 2
Views: 1069
Reputation: 76927
This would give you want you want --
df = pd.DataFrame({'Labels' : [["Cat", "Dog", "Horse"],
["Pig", "Fish", "Giraffe"]]})
df['Labels'].apply(lambda x: [y.lower() for y in x])
0 [cat, dog, horse]
1 [pig, fish, giraffe]
Name: Labels, dtype: object
But, like mentioned in comments, do you need to store the data is this way?
Upvotes: 1
Reputation: 394101
the following would work but generally it's a bad idea to store lists as data IMO:
In [18]:
df['labels'] = df['labels'].apply(lambda x: [w.lower() for w in x])
df
Out[18]:
labels
0 [cat, dog, horse]
1 [pig, fish, giraffe]
Upvotes: 2