[英]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 comprehension
与python str
函数lower
和replace
(如果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
函数lower
并replace
解决方案:
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
更改列名,则不可能,因为Index
( column 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(' ', '_'))
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.