繁体   English   中英

从txt形成熊猫数据框

[英]Forming pandas dataframe from txt

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

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

我想创建一个熊猫数据框,以便前500个数字构成第一列,接下来的500个数字构成第二列,依此类推(共10000列)。

如何在python中实现呢?

签出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的数字时才有效,但在这种情况下可以解决。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM