从列标题中删除特殊字符 [英] Remove special characters from column headers

查看:61
本文介绍了从列标题中删除特殊字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字典(data_final),其中包含数据框(健康,教育,经济等)。数据帧包含来自一个xlsx文件的数据。在其中一个数据框(经济)中,列名带有方括号和单引号。

I have a dictionary (data_final) of dataframes (health, education, economy,...). The dataframes contain data from one xlsx file. In one of the dataframes (economy), the column names have brackets and single quotes added to it.

data_final['economy'].columns = 
Index([                                ('Sr.No.',),
                                 ('DistrictName',),
                                  ('Agriculture',),
                            ('Forestry& Logging',),
                                      ('Fishing',),
                            ('Mining &Quarrying',),
                            ('ManufacturingMFG.',),
                               ('RegisteredMFG.',),
                                 ('Unregd. MFG.',),
                   ('Electricity,Gas & W.supply',),
                                 ('Construction',),
                    ('Trade,Hotels& Restaurants',),
                                     ('Railways',),
                      ('Transportby other means',),
                                      ('Storage',),
                                ('Communication',),
                           ('Banking &Insurance',),
       ('Real, Ownership of Dwel. B.Ser.& Legal',),
                         ('PublicAdministration',),
                                ('OtherServices',),
                                     ('TotalDDP',),
                           ('Population(In '00)',),
                        ('Per CapitaIncome(Rs.)',)],
      dtype='object')

我无法使用

data_final['economy']['('Construction',)']

给出错误-

SyntaxError: invalid syntax

我尝试使用replace删除括号-

I tried to use replace to remove the brackets -

data_final['economy'].columns = pd.DataFrame(data_final['economy'].columns).replace("(","",regex=True))

但这不会删除列名称中的错误。我如何从列名中删除所有这些特殊字符?

But this does not remove the error in column names. How can i remove all these special characters from column names?

推荐答案

看起来您的列名被导入/创建为元组。如果您尝试引用它们去掉括号,但最后留下逗号,会发生什么情况,

It looks as though your column names are being imported/created as tuples. What happens if you try and reference them removing the brackets, but leaving a comma on the end, like so

data_final['economy']['Construction',]

甚至带括号

data_final['economy'][('Construction',)]

这篇关于从列标题中删除特殊字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆