给定一个由空格分隔的数字组成的长字符串的.txt文件
12 0 84 9 5 1 49 ......
我想创建一个pandas数据帧,这样前500个数字构成第一列,下500个数字形成第二列,依此类推(总共10000列)。
这如何在python中实现呢?
发布于 2017-11-21 18:17:38
查看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的数字时,这才能起作用,但如果这是你的情况,这是可以修复的。
https://stackoverflow.com/questions/47410242
复制相似问题