如何小写数据框中除特定列之外的所有值

发布于 2025-01-09 11:47:13 字数 1688 浏览 1 评论 0原文

我已经搜索了一段时间,但似乎找不到这个小问题的答案。

我有这段代码来为小写值创建一个函数:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一身软味 2025-01-16 11:47:13

使用 Index.difference 用于按列表排除一些非数字列:

def content_consistent(df):
    cols = df.select_dtypes(object).columns.difference(['sub', 'status'])
    df[cols] = df[cols].apply(lambda x: x.str.lower())
    return df

Use Index.difference for exclude some non numeric columns by list:

def content_consistent(df):
    cols = df.select_dtypes(object).columns.difference(['sub', 'status'])
    df[cols] = df[cols].apply(lambda x: x.str.lower())
    return df
情释 2025-01-16 11:47:13

您可以使用列表理解排除那些列,如下所述

df = pd.DataFrame(df)

def content_consistent(df):
    cols = df.select_dtypes(object).columns
    cols = [x for x in cols if x not in ['sub', 'status']]
    df[cols] = df[cols].apply(lambda x: x.str.lower())
    return df

df = content_consistent(df)

You can exclude those columns with list comprehension as mentioned below

df = pd.DataFrame(df)

def content_consistent(df):
    cols = df.select_dtypes(object).columns
    cols = [x for x in cols if x not in ['sub', 'status']]
    df[cols] = df[cols].apply(lambda x: x.str.lower())
    return df

df = content_consistent(df)
睡美人的小仙女 2025-01-16 11:47:13

选择除 sub 和 Age 之外的列。将它们全部降低然后更新 df

df.update(df.filter(regex='[^subage]', axis=1).apply(lambda x:x.str.lower()))

Select columns except sub and age. make them all lower and then update the df

df.update(df.filter(regex='[^subage]', axis=1).apply(lambda x:x.str.lower()))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文