简体   繁体   中英

Pandas: Add column name to third column for multiple csv files

I have multiple csv files in same directory. Each csv file contains 3 columns but in 3rd column, column name is missing. To read all csv files I had to use error_bad_lines=False . Now, I want to add column name c3 to third column for multiple csv files.

Sample df:

v     info  
12    days    6
53    x       a
42    y       b 

Expected output:

    v    info   c3
0  12    days    6
1  53    x       a
2  42    y       b 

First, convert index "v" into column

df = df.reset_index()

Then, you can change the columns simply.

df.columns = ["v", "info", "c3"]

Finally,

import pandas as pd
for file in os.listdir(directory):
    if file.endswith(".csv"):
        df = pd.read_csv(file)
        df = df.reset_index() # this is option line
        df.columns = ["v", "info", "c3"]
        df.to_csv(file)


        

Not sure how your csv looks like, so I assumed that your csv would be:

v,info,
12,days,6
53,x,a
42,y,b

Whatever, you can load all the csv file in a directory and change column names as follows:

import pandas as pd
import glob

for f in glob.glob("C:\*.csv"):
    print(f)  # f is a file name
    df = pd.read_csv(f)
    df.reset_index() # add index column, 0, 1, 2, ...
    df.columns = ['v', 'info', 'c3'] # change column names
    df.to_csv(f)  # save it (overwriting)

You can see how to use glob to load files in a directory in detail here: https://www.geeksforgeeks.org/how-to-use-glob-function-to-find-files-recursively-in-python/

Use:

a = pd.DataFrame({'v': [12,53,42], 'info':['days','x','y'], '':[6,'a','b']})
cols = list(a.columns)[:2]
cols.append('col3')
a.columns = cols
a

output:

在此处输入图像描述

or, simply:

a.rename(columns={'':'new'})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM