从熊猫中的1个CSV文件读取多个表

时间:2018-11-08 20:16:41

标签: python pandas

假设我有一个这样的csv文件:

Name: Jack
Place: Binghampton
Age:27
Month,Sales,Revenue
Jan,51,$1000
Feb,20,$1050
Mar,100,$10000
### Blank File Space
### Blank File Space
Name: Jill
Place: Hamptonshire
Age: 49
Month,Sales,Revenue
Apr,11,$1000
May,55,$3000
Jun,23,$4600
### Blank File Space
### Blank File Space
...

并且文件内容如图所示均匀分布。我想将每个月,销售,收入部分读为自己的df。我知道我可以通过以下操作手动完成此操作:

df_Jack = pd.read_csv('./sales.csv', skiprows=3, nrows=3)
df_Jill = pd.read_csv('./sales.csv', skiprows=12, nrows=3)

我什至不担心df的名称,因为我认为自己可以做到,我只是真的不知道如何遍历间隔均匀的文件来查找销售记录并将其存储为唯一的dfs。

感谢您的任何帮助!

2 个答案:

答案 0 :(得分:2)

如何创建dfs列表?

from io import StringIO

csvfile = StringIO("""Name: Jack
Place: Binghampton
Age:27
Month,Sales,Revenue
Jan,51,$1000
Feb,20,$1050
Mar,100,$10000
### Blank File Space
### Blank File Space
Name: Jill
Place: Hamptonshire
Age: 49
Month,Sales,Revenue
Apr,11,$1000
May,55,$3000
Jun,23,$4600
### Blank File Space
### Blank File Space""")

df = pd.read_csv(csvfile, sep=',', error_bad_lines=False, names=['Month','Sales','Revenue'])

df1 = df.dropna().loc[df.Month!='Month']

listofdf = [df1[i:i+3] for i in range(0,df1.shape[0],3)]

print(listofdf[0])

输出:

  Month Sales Revenue
4   Jan    51   $1000
5   Feb    20   $1050
6   Mar   100  $10000

print(listofdf[1])

输出:

   Month Sales Revenue
13   Apr    11   $1000
14   May    55   $3000
15   Jun    23   $4600

答案 1 :(得分:2)

很显然,您可以这样做:

dfs = [pd.read_csv('./sales.csv', skiprows=i, nrows=3) for i in range(3, n, 9)]
# where n is your expected end line...

但是另一种方法是自己读取csv并将数据传递回pandas

with open('./sales.csv', 'r') as file:
    streaming = True
    while streaming:
        name = file.readline().rstrip().replace('Name: ','')
        for _ in range(2): file.readline()
        headers = file.readline().rstrip().split(',')
        data = [file.readline().rstrip().split(',') for _ in range(3)]
        dfs[name] = pd.DataFrame.from_records(data, columns=headers)
        for _ in range(2):
            streaming = file.readline()

与其他答案相比,我承认这是蛮残酷的,但确实有效。实际上,它在字典中为您提供了DataFrame的名称:

>>> dfs['Jack']

  Month Sales Revenue
0   Jan    51   $1000
1   Feb    20   $1050
2   Mar   100  $10000
>>> dfs['Jill']

  Month Sales Revenue
0   Apr    11   $1000
1   May    55   $3000
2   Jun    23   $4600