如何将多个熊猫数据帧中的各个列连接到带有for循环的预先存在的熊猫数据帧中?

时间:2020-10-30 18:01:11

标签: python pandas dataframe

我在运行for循环时遇到麻烦,该循环会将熊猫数据帧中的“时间”列连接到预先存在的数据帧中。

数据框看起来像:

西/东/长/纬度/时间/几何学

我的代码如下:

df = pd.read_csv('filepath\.csv') # preexisting dataframe that are similar to the rest

for files in glob.iglob(r'filepath\*.csv'):
    dfs = pd.read_csv(files) 
    dfs_new = dfs.iloc[:,[4]] # choose the time column to add in to df
concat_data = pd.concat([df, dfs_new], axis = 1)
concat_data.to_csv('master.csv')

但是,这只会将指定目录中的一个文件连接到预先存在的数据帧(df),而不是所有文件,例如:

西/东/长/纬度/时间/几何形状/时间2

...如何解决此问题,以获取目录中的所有文件,以将其“时间”列贡献给预先存在的数据框?

0 个答案:

没有答案
相关问题