I would like to read in multiple files from a directory, add an identifier for each file, and concatenate them into a single dataframe. Below is an example with two files but I am looking for a more efficient way to generalize the procedure to many files. I'd like to add an id column that identifies each file (so my question slightly differs from this)
Reprex:
# file1.csv
# file2.csv
import pandas as pd
df1 = pd.read_csv("file1.csv")
df["id"] = "file1"
df2 = pd.read_csv("file2.csv")
df2["id"] = "file2"
df_all = pd.concat[df, df2]
I would also like to use it for .txt files like so:
>>> %cat 'file1.txt'
A|B|C
1|0.5|good
2|0.2|bad
3|0.6|good
4|0.4|ok
>>> %cat 'file2.txt'
A|B|C
1|0.2|bad
2|0.5|ok
3|0.9|good
4|0.3|ok
5|0.7|bad