代码之家  ›  专栏  ›  技术社区  ›  dythe

Lambda将列映射为大写[重复]

  •  0
  • dythe  · 技术社区  · 6 年前

    我无法将大写应用于数据帧中的列。

    数据帧是 df .

    1/2 ID 是需要应用大写的柱头。

    rrr123 是值之一。

    df['1/2 ID'] = map(str.upper, df['1/2 ID'])
    

    我有个错误:

    TypeError: descriptor 'upper' requires a 'str' object but received a 'unicode' error.

    如何将大写字母应用于数据帧列中的前三个字母 测向 ?

    0 回复  |  直到 9 年前
        1
  •  29
  •   Jansen Simanullang Di Zou    5 年前

    这应该起作用:

    df['1/2 ID'] = map(lambda x: str(x).upper(), df['1/2 ID'])
    

    你应该想要所有的 columns 名称采用大写格式:

    df.columns = map(lambda x: str(x).upper(), df.columns)
    
        2
  •  86
  •   cs95 abhishek58g    6 年前

    如果您的pandas版本是最新版本,那么您可以只使用矢量化的string方法 upper :

    df['1/2 ID'] = df['1/2 ID'].str.upper()
    

        3
  •  9
  •   bakkal    9 年前

    str.upper() 想要一个普通的老Python 2字符串

    unicode.upper() 将需要unicode而不是字符串(或者您得到TypeError:描述符'upper'需要'unicode'对象,但收到'str')

    所以我建议使用duck输入和call .upper()

    df['1/2 ID'].apply(lambda x: x.upper(), inplace=True)