简体   繁体   中英

python read csv files with same basename and save as different dataframes

I have 20 csv files with the same basename and a number from 100 to 2000 with an increment of 100 between files, such that samp_100.csv, samp_200.csv, samp_300.csv, ..., samp_1900.csv, samp_2000.csv.

I am trying to read these files into python. I am trying the following.

T = np.arange(100,2100,100)
for i in T: 
    df = pd.read_csv("samp_{i}.csv".format(i=i))

Although I do not get an error, the files aren't read in the correct order from 100 to 2000. When I use df.head, I do not see the first lines of the file samp_100.csv. Also the files are concatenated into a single file called df. Is there an equivalent way to achieve this but instead have 20 separate dataframes with the names df_100, df_200, ..., df_1900, df_2000?

You needpandas.concat .

Try this:

import numpy as np
import pandas as pd

T = np.arange(100,2100,100)

list_of_df = []
for i in T:
    temp_df = pd.read_csv(f"samp_{i}.csv")
    list_of_df.append(temp_df)
    
df = pd.concat(list_of_df, axis=0, ignore_index=True)

If you need to add a column with the name of the .csv , include the line below after calling pandas.read_csv inside the loop.

temp_df.insert(0, "filename", f"samp_{i}")

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