EDIT 我想通了 - 比我想象的要容易 - 只需将索引设置为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)
以我可以使用的格式输出! (请参阅下面的链接了解所需的格式)
###结束编辑我有一个文件,我已设法带到所需的格式;原始数据看起来像original data 我想要的新格式是这样的:desired format
环顾四周,我看到了数据透视表功能 - (df是保存原始数据的数据帧)
df_desired = df.pivot_table(['R', 'I'], ['X', 'Y', 'Z','#','Er', 'H'], 'Fr')
但是这只给了我基于Fr的R和I的值(这就是我想要的,但我也想要所有其他数据)
任何提示?
答案 0 :(得分: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)