从 Panda groupby 读取 txt 到多列

Read txt from Panda groupby to multiple columns

好的,所以我写了一个带有 pd.groupby 数据框的 txt。现在我需要打开它。

到目前为止我得到的是:

f = open('C:/MDH.txt', 'r')
reg = f.read()
rege = np.asarray(reg)

但这只是给了我一个很长的行,其中行在原始数组中结束,但没有对 422472 个元素进行定界。举个例子:

array('4.498000000000000000e+03 5.866666666666667140e+00 1.989999999999999858e+01 6.763333333333333997e+01 3.600000000000000000e+01 0.000000000000000000e+00 -7.165617522142724738e+00 2.800000000000000000e+01 4.000000000000000000e+00 3.000000000000000000e+00 0.000000000000000000e+00 0.000000000000000000e+00\n4.495000000000000000e+03 5.799999999999999822e+00

我需要将其恢复为 12 列乘 35.206 行的数组,用于 txt 中的 422472 个元素。

此外,我需要知道如何在 python 中将 1.989999999999999858e+01 转换为 19.89。

我不需要原始groupby的索引,只需要按行的列。

您可以使用 np.loadtxt 自动读取您的文件并解析其内容。

首先,我创建了一些虚拟数据并保存:

In [627]: x = np.random.randn(35206, 12)

In [629]: np.savetxt('MDH.txt', x)

接下来,我可以使用 np.loadtxt:

加载它
In [630]: y = np.loadtxt('C:/MDH.txt')

In [631]: y.shape
Out[631]: (35206, 12)

In [632]: y.dtype
Out[632]: dtype('float64')