Lowercase names of data columns within a list

I have several csvs, so I used glob and read_csv, add each one to the list, and then concatenate all of them.

My question is, how can I access column names and lowercase ones?

EDIT:

    allfiles = glob.glob("*.csv", )
    dataframes = []
    for file in allfiles :
        dataframes.append(pd.read_csv(file, sep=";", decimal=","))
    df = pd.concat(dataframes)

      

Thank!

+3


source to share


2 answers


I think you need str.lower

:

df.columns = df.columns.str.lower()

      




allfiles = glob.glob("*.csv", )
dataframes = []
for file in allfiles :
    df = pd.read_csv(file, sep=";", decimal=",")
    df.columns = df.columns.str.lower()
    dataframes.append(df)
df = pd.concat(dataframes)

      

+3


source


To rename columns you can use rename()

DataFrames method .

Here's an example:

df.rename(columns={colname:colname.lower() for colname in df.axes[1]})

      



It uses a list of column names ( df.axes[1]

) to create a dict

mapping of the old column names to their newer, lower-sized versions. The method then rename()

returns a copy of the dataframe with the renamed columns.

Note that it rename()

has a flag inplace

if you would rather edit the data block in place than return a copy.

+1


source







All Articles