Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding a column to dataframe while reading csv files [pandas]

Tags:

python

pandas

I'm reading multiple csv files and combining them into a single dataframe like below:

pd.concat([pd.read_csv(f, encoding='latin-1') for f in glob.glob('*.csv')],
         ignore_index=False, sort=False)

Problem:

I want to add a column that doesn't exist in any csv (to the dataframe) based on the csv file name for every csv file that is getting concatenated to the dataframe. Any help will be appreciated.

like image 265
m-ketan Avatar asked Sep 07 '25 17:09

m-ketan


1 Answers

glob.glob returns normal string so you can just add a column to every individual dataframe in a loop.

Assuming you have files df1.csv and df2.csv in your directory:

import glob
import pandas as pd

files = glob.glob('df*csv')
dfs = []
for file in files:
    df = pd.read_csv(file)
    df['filename'] = file
    dfs.append(df)
df = pd.concat(dfs, ignore_index=True)
df

    a   b   filename
0   1   2   df1.csv
1   3   4   df1.csv
2   5   6   df2.csv
3   7   8   df2.csv
like image 163
pieca Avatar answered Sep 10 '25 15:09

pieca