简体   繁体   English

python 读取具有相同基本名称的 csv 文件并保存为不同的数据帧

[英]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 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.我正在尝试将这些文件读入 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.虽然我没有收到错误,但文件没有按照从 100 到 2000 的正确顺序读取。当我使用 df.head 时,我看不到文件 samp_100.csv 的第一行。 Also the files are concatenated into a single file called df.这些文件也被连接成一个名为 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?是否有等效的方法来实现这一点,但有 20 个单独的数据帧,名称为 df_100、df_200、...、df_1900、df_2000?

You needpandas.concat .你需要pandas.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.如果您需要添加名称为.csv的列,请在循环内调用pandas.read_csv后包含以下行。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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