如何在Python中读取Pandas单独数据帧的多个文件 [英] How to Read multiple files in Python for Pandas separate dataframes

查看:51
本文介绍了如何在Python中读取Pandas单独数据帧的多个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将6个文件读入7个不同的数据帧中,但无法弄清楚该怎么做.文件名可以是完全随机的,也就是说我知道文件,但是它不像data1.csv data2.csv.

I am trying to read 6 files into 7 different data frames but I am unable to figure out how should I do that. File names can be complete random, that is I know the files but it is not like data1.csv data2.csv.

我尝试使用类似这样的东西:

I tried using something like this:

import sys
import os
import numpy as np
import pandas as pd
from datetime import datetime, timedelta
f1='Norway.csv'
f='Canada.csv'
f='Chile.csv'

Norway = pd.read_csv(Norway.csv)
Canada = pd.read_csv(Canada.csv)
Chile = pd.read_csv(Chile.csv )

我需要读取不同数据帧中的多个文件.当我处理一个文件之类的文件时,它工作正常

I need to read multiple files in different dataframes. it is working fine when I do with One file like

file='Norway.csv
Norway = pd.read_csv(file)

我收到错误消息:

NameError: name 'norway' is not defined

推荐答案

您可以将所有.csv文件读取到一个数据框中.

You can read all the .csv file into one single dataframe.

for file_ in all_files:
    df = pd.read_csv(file_,index_col=None, header=0)
    list_.append(df)

# concatenate all dfs into one
big_df = pd.concat(dfs, ignore_index=True)

,然后将大数据框分割成多个(在您的情况下为7).例如,-

and then split the large dataframe into multiple (in your case 7). For example, -

import numpy as np
num_chunks = 3  
df1,df2,df3 = np.array_split(big_df,num_chunks)

希望这会有所帮助.

这篇关于如何在Python中读取Pandas单独数据帧的多个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆