简体   繁体   English

我可以使用applymap来更改数据帧的变量名称

[英]can I use applymap to change variable names of dataframe

If I want to change variable names in a data frame using pandas can I change the names without using pandas.df.rename() function but by using applymap() For example 如果我想使用pandas更改数据框中的变量名称,我可以在不使用pandas.df.rename()函数的情况下更改名称,但使用applymap()

Registrar   Enrolment Agency    State   District    Sub District    Pin Code    Gender
Allahabad Bank  Tera Software Ltd   Jharkhand   Ranchi  Namkum  834003  M
Allahabad Bank  Tera Software Ltd   Jharkhand   Ranchi  Ranchi  834004  F
Allahabad Bank  Vakrangee Softwares Limited Gujarat Surat   Nizar   394380  M

I need to fill spaces in the variable names in the above data frame with "_" and all the variable names should be in lower case 我需要用“_”填充上面数据框中变量名的空格,并且所有变量名都应该是小写的

Function applymap is used for change all values of DataFrame element-wise, so for change column names are another methods. 函数applymap用于applymap更改DataFrame所有值,因此对于更改列名是另一种方法。

I think you need list comprehension with python str function lower and replace (it does not work if NaN ): 我认为你需要list comprehensionpython str函数lowerreplace (如果NaN它不起作用):

df.columns = [col.lower().replace(' ', '_') for col in df.columns]
print (df)
        registrar     enrolment_agency            state district sub_district  \
0  Allahabad Bank    Tera Software Ltd        Jharkhand   Ranchi       Namkum   
1  Allahabad Bank    Tera Software Ltd        Jharkhand   Ranchi       Ranchi   
2  Allahabad Bank  Vakrangee Softwares  Limited Gujarat    Surat        Nizar   

   pin_code gender  
0    834003      M  
1    834004      F  
2    394380      M  

Or solution with pandas str function lower and replace : 或者用pandas str函数lowerreplace解决方案:

df.columns = df.columns.str.replace(' ', '_').str.lower()
print (df)
        registrar     enrolment_agency            state district sub_district  \
0  Allahabad Bank    Tera Software Ltd        Jharkhand   Ranchi       Namkum   
1  Allahabad Bank    Tera Software Ltd        Jharkhand   Ranchi       Ranchi   
2  Allahabad Bank  Vakrangee Softwares  Limited Gujarat    Surat        Nizar   

   pin_code gender  
0    834003      M  
1    834004      F  
2    394380      M  

EDIT: 编辑:

If need change column names by applymap it is impossible because this function is not implemented for Index ( column names ). 如果需要通过applymap更改列名,则不可能,因为Indexcolumn names )没有实现此功能。 But if really want something similar use map : 但如果真的想要类似的东西使用map

df.columns = df.columns.map(lambda col: col.lower().replace(' ', '_'))
print (df)
        registrar     enrolment_agency            state district sub_district  \
0  Allahabad Bank    Tera Software Ltd        Jharkhand   Ranchi       Namkum   
1  Allahabad Bank    Tera Software Ltd        Jharkhand   Ranchi       Ranchi   
2  Allahabad Bank  Vakrangee Softwares  Limited Gujarat    Surat        Nizar   

   pin_code gender  
0    834003      M  
1    834004      F  
2    394380      M  

You could do that in 3 ways. 你可以用3种方式做到这一点。

Rewriting the columns 重写列

df.columns = [x.lower().replace(' ', '_') for x in df.columns] 

Using string methods 使用字符串方法

df.columns = df.columns.str.lower().str.replace(' ', '_')

Using rename 使用rename

df.rename(columns=lambda x: x.lower().replace(' ', '_'))

Try this: 尝试这个:

df.applymap(lambda x: str(x).lower().replace(' ', '_'))

applymap() will change the spaces to underscore in the data. applymap()会将数据中的空格更改为下划线。 If you want to change it in columns, then you can take jezrael's or John's answer. 如果你想在列中更改它,那么你可以采取jezrael或John的答案。

在此输入图像描述

Seriously... pick @JohnGalt's answer. 说真的......选择@ JohnGalt的回答。 rename is the way to go. rename是要走的路。

But because I like to try to add something other answers haven't covered yet: 但是因为我想尝试添加尚未涵盖的其他答案:

Use split then join 使用split然后join

df.columns = df.columns.str.lower().str.split().str.join('_')
df

在此输入图像描述

Split and join has an advantage of replacing more than 1 space with underscore _ 拆分和连接具有使用下划线替换多于1个空格的优势_

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM