pandas srt.lower()不适用于数据框列 [英] pandas srt.lower() not working on dataframe column
问题描述
我正在使用Kaggle提供的Titanic数据集.我在一个数据帧中有它,我想将性别"列的大小写更改为小写.我正在使用以下代码
I'm working with the Titanic dataset available from Kaggle. I have it in a dataframe and i want to change the case of the "sex" column to lowercase. I'm using the following code
import pandas as pd
df = pd.read_csv('titanic.csv')
print dfFull['sex'].unique()
df.sex.str.lower()
#check if it worked
print df['sex'].unique()
并尝试
df['sex'].str.lower()
但是当我运行df['sex'].unique()
时,我得到三个唯一值[male, female, Female]
.
but when I run df['sex'].unique()
I get three unique values [male, female, Female]
.
为什么我的代码不降低字符串的大小写并将其保存回数据帧,以便从.unique
方法获得[male, female]
的值?
Why does my code not lower the case of the strings and save it back to the dataframe so i get [male, female]
of from the .unique
method?
推荐答案
str.lower()
不会修改现有列.它只是返回一个应用了小写转换的新Series.如果要覆盖原始列,则需要将结果分配回它:
str.lower()
does not modify the existing column. It just returns a new Series with the lowercase transform applied. If you want to overwrite the original column, you need to assign the result back to it:
df['sex'] = df.sex.str.lower()
这篇关于 pandas srt.lower()不适用于数据框列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!