从txt形成pandas数据帧

时间:2017-11-21 10:02:38

标签: python pandas dataframe

给定一个.txt文件,该文件由一长串以空格分隔的数字组成

12 0 84 9 5 1 49 ......

我想创建一个pandas数据帧,前500个数字组成第一列,接下来的500个数字组成第2列,等等(总共10000列)。

如何在python中实现?

1 个答案:

答案 0 :(得分:0)

结帐numpy.reshape。 10个数字的示例

with open('xx.txt') as f:
    txt = f.read()

import pandas as pd
import numpy as np

txt_sep = np.array(txt.split())

txt_sep = txt_sep.reshape(5,-1)

df = pd.DataFrame(txt_sep)

输出:

df
   0   1
0  1   2
1  3   4
2  5   6
3  7   8
4  9  10

在你的情况下:

txt_sep = txt_sep.reshape(500,-1)

但是,只有当你有10000x500的数字时,这才有效,但如果你的情况是可以修复的话。

相关问题