如何小写数据框中除特定列之外的所有值
我已经搜索了一段时间,但似乎找不到这个小问题的答案。
我有这段代码来为小写值创建一个函数:
df = {'name':['AL', 'EL', 'NAILA', 'DORI', 'KAKAEKA', 'GENTA', 'RUBY'],
'living':['lagoa','sangiang','penjaringan','warakas','jonggol','cikarang', 'cikarang'],
'food':['PIZZA','MEATBALL','CHICKEN','CAKE','CAKE','ONION','NOODLE'],
'sub':['KOTA','KAB','WILAYAH','KAB','DAERAH','KOTA','WILAYAH'],
'job':['Chef','Teacher','Police','Doctor','Students','Programmer','Lecturer'],
'side_job':['Designer','Nurse','Designer','Programmer','Programmer','Teacher','Mentor'],
'status':['Single','Single','Married','Single','Single','Divorced','Married'],
'age':[20,25,20,18,25,40,37]
}
df = pd.DataFrame(df)
def content_consistent(df):
cols = df.select_dtypes(object).columns
df[cols] = df[cols].apply(lambda x: x.str.lower())
return df
df = content_consistent(df)
结果显示所有值都是小写的,但我想要的是一些列不要像“sub”和“status”列一样小写
但我实际上期望这个输出简单的代码不使用循环
name living food sub job side_job status age
0 al lagoa pizza KOTA chef designer Single 20
1 el sangiang meatball KAB teacher nurse Single 25
2 naila penjaringan chicken WILAYAH police designer Married 20
3 dori warakas cake KAB doctor programmer Single 18
4 kakaeka jonggol cake DAERAH students programmer Single 25
5 genta cikarang onion KOTA programmer teacher Divorced 40
6 ruby cikarang noodle WILAYAH lecturer mentor Married 37
I've been searching around for a while now, but I can't seem to find the answer to this small problem.
I have this code to make a function for lowercase values:
df = {'name':['AL', 'EL', 'NAILA', 'DORI', 'KAKAEKA', 'GENTA', 'RUBY'],
'living':['lagoa','sangiang','penjaringan','warakas','jonggol','cikarang', 'cikarang'],
'food':['PIZZA','MEATBALL','CHICKEN','CAKE','CAKE','ONION','NOODLE'],
'sub':['KOTA','KAB','WILAYAH','KAB','DAERAH','KOTA','WILAYAH'],
'job':['Chef','Teacher','Police','Doctor','Students','Programmer','Lecturer'],
'side_job':['Designer','Nurse','Designer','Programmer','Programmer','Teacher','Mentor'],
'status':['Single','Single','Married','Single','Single','Divorced','Married'],
'age':[20,25,20,18,25,40,37]
}
df = pd.DataFrame(df)
def content_consistent(df):
cols = df.select_dtypes(object).columns
df[cols] = df[cols].apply(lambda x: x.str.lower())
return df
df = content_consistent(df)
the result shows all values to be lowercase, but what I want is some columns not to be lowercase like 'sub' and 'status' columns
But I am actually expecting this output with the simple code not use looping
name living food sub job side_job status age
0 al lagoa pizza KOTA chef designer Single 20
1 el sangiang meatball KAB teacher nurse Single 25
2 naila penjaringan chicken WILAYAH police designer Married 20
3 dori warakas cake KAB doctor programmer Single 18
4 kakaeka jonggol cake DAERAH students programmer Single 25
5 genta cikarang onion KOTA programmer teacher Divorced 40
6 ruby cikarang noodle WILAYAH lecturer mentor Married 37
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用
Index.difference
用于按列表排除一些非数字列:Use
Index.difference
for exclude some non numeric columns by list:您可以使用列表理解排除那些列,如下所述
You can exclude those columns with list comprehension as mentioned below
选择除 sub 和 Age 之外的列。将它们全部降低然后更新 df
Select columns except sub and age. make them all lower and then update the df