-2

编辑我想通了 - 比我想象的要容易 - 只需将 index 设置为 true - 见下文

import numpy as npg
import pandas as pd
original_data = np.array([[1,0,0,10,1530,0.1,2,-49.0756686364,163.856504545],
                 [1,0,0,10,8250,0.1,2,-84.7795213636,264.205363636],
[1,0,0,10,20370,0.1,2,-245.585863636,662.467227273],
[1,0,0,10,33030,0.1,2,-290.468136364,1107.19936364],
[1,0,0,10,47970,0.1,2,-308.668681818,1584.18090909],
[1,0,0,10,63090,0.1,2,-244.954186364,1932.43454545],
[1,0,0,10,93090,0.1,2,-9.5453963636,1794.01636364],
[1.5,0.5,0,11,1530,0.1,2,-36.28640875,136.9883],
[1.5,0.5,0,11,8250,0.1,2,-83.2991125,231.58275],
[1.5,0.5,0,11,20370,0.1,2,-227.956625,757.027125],
[1.5,0.5,0,11,33030,0.1,2,-301.754875,1045.55275],
[1.5,0.5,0,11,47970,0.1,2,-291.517625,1534.11625],
[1.5,0.5,0,11,63090,0.1,2,-257.7195,1904.9475],
[1.5,0.5,0,11,93090,0.1,2,-32.52868875,1756.22]])
df =pd.DataFrame(original_data,  columns=['X', 'Y', 'Z', '#','Fr', 'Er', 'V', 'R', 'I'])
df1 = df.pivot_table(['R', 'I'], ['X', 'Y', 'Z', '#','Er', 'V'], 'Fr')
df1.to_csv('test.dat', sep='\t', header=True, index=True)

并且以我可以使用的格式输出!(有关所需格式,请参见下面的链接)

###结束编辑

我有一个文件,我设法将其转换为所需的格式;原始数据看起来像原始数据 我想要的新格式是这样的:想要的格式

环顾四周,我看到了数据透视表函数——(df 是保存原始数据的数据框)

df_desired = df.pivot_table(['R', 'I'], ['X', 'Y', 'Z','#','Er', 'H'], 'Fr')

但这仅给了我基于 Fr 的 R 和 I 的值(这是我想要的,但我也希望它与所有其他数据一起使用)

任何提示?

4

1 回答 1

2

这段代码对我有用!

import numpy as np
import pandas as pd
original_data = np.array([[1,0,0,10,1530,0.1,2,-49.0756686364,163.856504545],
                 [1,0,0,10,8250,0.1,2,-84.7795213636,264.205363636],
[1,0,0,10,20370,0.1,2,-245.585863636,662.467227273],
[1,0,0,10,33030,0.1,2,-290.468136364,1107.19936364],
[1,0,0,10,47970,0.1,2,-308.668681818,1584.18090909],
[1,0,0,10,63090,0.1,2,-244.954186364,1932.43454545],
[1,0,0,10,93090,0.1,2,-9.5453963636,1794.01636364],
[1.5,0.5,0,11,1530,0.1,2,-36.28640875,136.9883],
[1.5,0.5,0,11,8250,0.1,2,-83.2991125,231.58275],
[1.5,0.5,0,11,20370,0.1,2,-227.956625,757.027125],
[1.5,0.5,0,11,33030,0.1,2,-301.754875,1045.55275],
[1.5,0.5,0,11,47970,0.1,2,-291.517625,1534.11625],
[1.5,0.5,0,11,63090,0.1,2,-257.7195,1904.9475],
[1.5,0.5,0,11,930901,0,0,10,63090,0.1,2,-244.954186364,1932.43454545]

df =pd.DataFrame(original_data,  columns=['X', 'Y', 'Z', '#','Fr', 'Er', 'V', 'R', 'I'])
df1 = df.pivot_table(['R', 'I'], ['X', 'Y', 'Z', '#','Er', 'V'], 'Fr')
df1.to_csv('test.dat', sep='\t', header=True, index=True)
于 2013-10-02T02:48:19.270 回答