为每个 header 创建单独的 pandas 数据帧
Make seperate pandas data frames per header
所以我有一个 csv,其中包含每天由 header 分隔的数据。无论如何我可以在每次程序点击 header 时制作单独的 pandas dfs?
数据基本是这样的
#dateinformation
data1, data2, data3
data4, data5, data6
#dateinformation
真实的 csv 示例是这个
#7240320140101002301 131
21101400B 86 12B 110 325 25
10100000 200B 6B 110 325 77
20 95300 -9999 -27B 100-9999-9999
10 92500 820B -39B 90 290
.....
#7240320140102002301
21101400B 86 14B 110 325 25
10100000 200B 2B 110 325 77
20 95300 -9999 -85B 100-9999-9999
10 92500 820B -25B 90 290
我已经很好地设置了实际数据的格式。我只需要一些帮助来了解如何在 csv
中分离出不同的集合
(下面的代码基于以“#”开头的 header 行)
我想理论上你会用 read_table 和 chunksize 来做到这一点,但实际上我很难让它很好地工作,因为每行的字段数量不同。以下内容相当简单,但我不得不求助于 iterrows。
In [1435]: df_list = []
...: df = pd.DataFrame()
...: j = 0
...: foo = pd.read_csv('foo.txt',sep=' *',names=list('abcdef'))
...: for i, row in foo.ix[1:].iterrows():
...: if row[0][0] == '#':
...: df_list.append(df)
...: df = pd.DataFrame()
...: else:
...: df = df.append(row)
...: df_list.append(df)
In [1436]: df_list[0]
Out[1436]:
a b c d e f
1 21101400B 86 12B 110 325 25
2 10100000 200B 6B 110 325 77
3 20 95300 -9999 -27B 100-9999-9999 NaN
4 10 92500 820B -39B 90 290
In [1437]: df_list[1]
Out[1437]:
a b c d e f
6 21101400B 86 14B 110 325 25
7 10100000 200B 2B 110 325 77
8 20 95300 -9999 -85B 100-9999-9999 NaN
9 10 92500 820B -25B 90 290
此答案基于每个 'frame' 包含相同行数的假设
首先我们用 pandas read_csv() 读取文件。我们利用评论参数省略您的每个 headers 并只读入数据
df = pd.read_csv('data.txt', comment='#', delim_whitespace=True, header=None)
df
0 1 2 3 4 5
0 21101400B 86 12B 110 325 25
1 10100000 200B 6B 110 325 77
2 20 95300 -9999 -27B 100-9999-9999 NaN
3 10 92500 820B -39B 90 290
4 21101400B 86 14B 110 325 25
5 10100000 200B 2B 110 325 77
6 20 95300 -9999 -85B 100-9999-9999 NaN
7 10 92500 820B -25B 90 290
然后一个for循环来解析每个帧并将其存储在一个列表中。我假设行数 = 4
frames = []
for begin in range(0,len(df),4):
frames.append(df[begin:begin+4])
frames[0]
0 1 2 3 4 5
0 21101400B 86 12B 110 325 25
1 10100000 200B 6B 110 325 77
2 20 95300 -9999 -27B 100-9999-9999 NaN
3 10 92500 820B -39B 90 290
所以我有一个 csv,其中包含每天由 header 分隔的数据。无论如何我可以在每次程序点击 header 时制作单独的 pandas dfs?
数据基本是这样的
#dateinformation
data1, data2, data3
data4, data5, data6
#dateinformation
真实的 csv 示例是这个
#7240320140101002301 131
21101400B 86 12B 110 325 25
10100000 200B 6B 110 325 77
20 95300 -9999 -27B 100-9999-9999
10 92500 820B -39B 90 290
.....
#7240320140102002301
21101400B 86 14B 110 325 25
10100000 200B 2B 110 325 77
20 95300 -9999 -85B 100-9999-9999
10 92500 820B -25B 90 290
我已经很好地设置了实际数据的格式。我只需要一些帮助来了解如何在 csv
中分离出不同的集合(下面的代码基于以“#”开头的 header 行)
我想理论上你会用 read_table 和 chunksize 来做到这一点,但实际上我很难让它很好地工作,因为每行的字段数量不同。以下内容相当简单,但我不得不求助于 iterrows。
In [1435]: df_list = []
...: df = pd.DataFrame()
...: j = 0
...: foo = pd.read_csv('foo.txt',sep=' *',names=list('abcdef'))
...: for i, row in foo.ix[1:].iterrows():
...: if row[0][0] == '#':
...: df_list.append(df)
...: df = pd.DataFrame()
...: else:
...: df = df.append(row)
...: df_list.append(df)
In [1436]: df_list[0]
Out[1436]:
a b c d e f
1 21101400B 86 12B 110 325 25
2 10100000 200B 6B 110 325 77
3 20 95300 -9999 -27B 100-9999-9999 NaN
4 10 92500 820B -39B 90 290
In [1437]: df_list[1]
Out[1437]:
a b c d e f
6 21101400B 86 14B 110 325 25
7 10100000 200B 2B 110 325 77
8 20 95300 -9999 -85B 100-9999-9999 NaN
9 10 92500 820B -25B 90 290
此答案基于每个 'frame' 包含相同行数的假设
首先我们用 pandas read_csv() 读取文件。我们利用评论参数省略您的每个 headers 并只读入数据
df = pd.read_csv('data.txt', comment='#', delim_whitespace=True, header=None)
df
0 1 2 3 4 5
0 21101400B 86 12B 110 325 25
1 10100000 200B 6B 110 325 77
2 20 95300 -9999 -27B 100-9999-9999 NaN
3 10 92500 820B -39B 90 290
4 21101400B 86 14B 110 325 25
5 10100000 200B 2B 110 325 77
6 20 95300 -9999 -85B 100-9999-9999 NaN
7 10 92500 820B -25B 90 290
然后一个for循环来解析每个帧并将其存储在一个列表中。我假设行数 = 4
frames = []
for begin in range(0,len(df),4):
frames.append(df[begin:begin+4])
frames[0]
0 1 2 3 4 5
0 21101400B 86 12B 110 325 25
1 10100000 200B 6B 110 325 77
2 20 95300 -9999 -27B 100-9999-9999 NaN
3 10 92500 820B -39B 90 290