0

data sample from CSV file

Model,Displ,Cyl,Trans,Drive,Fuel,Cert Region,Stnd,Stnd Description,Underhood ID,Veh Class,Air Pollution Score,City MPG,Hwy MPG,Cmb MPG,Greenhouse Gas Score,SmartWay,Comb CO2

ACURA RDX,3.5,6,SemiAuto-6,2WD,Gasoline,FA,T3B125,Federal Tier 3 Bin 125,JHNXT03.5GV3,small SUV,3,20,28,23,5,No,386
import pandas as pd
df_18 = pd.read_csv('file name')

request: Rename all column labels to replace spaces with underscores and convert everything to lowercase. below code did work, and I don't know why

df_18.rename(str.lower().str.strip().str.replace(" ","_"),axis=1,inplace=True)
ThePyGuy
  • 17,779
  • 5
  • 18
  • 45

3 Answers3

1

You can directly assign the list of column names to pandas.DataFrame.columns; you can perform the required operations i.e. lower, strip, and replace in a list-comprehension for each column names, and assign it back to the dataframe.columns

df_18.columns = [col.lower().strip().replace(" ","_") for col  in df_18]

OUTPUT:

       model  displ  cyl  ... greenhouse_gas_score smartway comb_co2
0  ACURA RDX    3.5    6  ...                    5       No      386
[1 rows x 18 columns]
ThePyGuy
  • 17,779
  • 5
  • 18
  • 45
0
for column in df_18.columns:
    new_column_name = column.lower().strip().replace(" ","_")

    if new_column_name != column:
        df_18[new_column_name] = df_18[column]
        del df_18[column]
qwwqwwq
  • 6,999
  • 2
  • 26
  • 49
  • This approach has bug. You are deleting the column in the end. If source column is already in lowercase and does not have _, it will get deleted. – Ashutosh Aug 02 '21 at 13:56
  • 1
    Fair point, edited and added a conditional to prevent that – qwwqwwq Aug 03 '21 at 01:00
0

There are many ways to rename the column,

reference for renaming columns

reference for replace string

you can use the below code.

df_18.columns=[col.lower().replace(" ","_")  for col in df_18.columns]
Shubhankar
  • 72
  • 1
  • 10
  • i received this as a solution, but i can't understand how did it work df_18.rename(columns=lambda x: x.strip().lower().replace(" ", "_"), inplace=True) how did the function got the x attribute?, and how all columns changed – George Sedrak Aug 02 '21 at 13:52
  • In rename method of dataframe, column parameter requires, a dictionary i.e., old_value: new_value, or it can have a function which will map the column name , and inplace=True did changesa as permanent, (default is false) https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.rename.html – Shubhankar Aug 02 '21 at 14:06
  • i understand the first part, but "or it can have a function which will map the column name" i cant relate it with the previous code!! – George Sedrak Aug 02 '21 at 14:15
  • lambda x: x.strip().lower().replace(" ", "_"), this lambda function will get the columns names of df_18, and it will return in the corresponding column name with the lowercase and spaces will be replaced by _ – Shubhankar Aug 03 '21 at 04:57