Remove special characters from column headers

2024/10/11 23:21:20

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')

I cannot reference any column using

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

gives error -

SyntaxError: invalid syntax

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?

Answer

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',]

or even with the brackets

data_final['economy'][('Construction',)]
https://en.xdnf.cn/q/118266.html

Related Q&A

Python Flask application getting OPTIONS instead of POST

I have a python Flask listener waiting on port 8080. I expect another process to make a series of POSTs to this port.The code for listener is as follows.#!/usr/bin/env python2 from __future__ import pr…

Raspberry pi:convert fisheye image to normal image using python

I have attached the USB webcam with raspberry pi to capture image and write code to send it using mail. It captures image using fswebcam commamnd so code for capture image in python script is :subproce…

modifying python daemon script, stop does not return OK (but does kill the process)

Following on from the previous post, the script now start and stops the python script (and only that particular script) correctly but does not report the OK back to the screen...USER="root" A…

fulfill an empty dataframe with common index values from another Daframe

I have a daframe with a series of period 1 month and frequency one second.The problem the time step between records is not always 1 second.time c1 c2 2013-01-01 00:00:01 5 3 2013-01-0…

How to mix numpy slices to list of indices?

I have a numpy.array, called grid, with shape:grid.shape = [N, M_1, M_2, ..., M_N]The values of N, M_1, M_2, ..., M_N are known only after initialization.For this example, lets say N=3 and M_1 = 20, M_…

Visualize strengths and weaknesses of a sample from pre-trained model

Lets say Im trying to predict an apartment price. So, I have a lot of labeled data, where on each apartment I have features that could affect the price like:city street floor year built socioeconomic s…

Scrapy get result in shell but not in script

one topic again ^^ Based on recommendations here, Ive implemented my bot the following and tested it all in shell :name_list = response.css("h2.label.title::text").extract()packaging_list = r…

How to find a source when a website uses javascript

What I want to achieve I am trying to scrape the website below using Beautiful-soup and when I load the page it does not give the table that shows various quotes. In my previous posts folks have helped…

How to print a list of dicts as an aligned table?

So after going through multiple questions regarding the alignment using format specifiers I still cant figure out why the numerical data gets printed to stdout in a wavy fashion.def create_data(soup_ob…

abstract classes in python: Enforcing type

My question is related to this question Is enforcing an abstract method implementation unpythonic? . I am using abstract classes in python but I realize that there is nothing that stops the user from …