英文:
Integration of X and Y in a XYZV csv file
问题
我正在处理一个项目,基本上在一个CSV文件中有X、Y、Z和电压维度。我希望将这个XYZV网格缩减为XYV网格,以制作一个2D图。这需要我取相同的每对X、Y并在不同的Z值下平均电压列。
即:
X Y. Z. V
1. 0. 1. 10
1. 0. 1.5. 5
1. 0. 0. 1
1. 1. 1. 1
变成:
X. Y. V.
1. 0. 5.333
1. 1. 1
我应该如何在Python中使用pandas或csv读取CSV文件来实现这个目标?
英文:
I am working on a project which essentially has X,Y,Z,Voltage dimensions within a csv file. I am hoping to take this XYZV grid and reduce it to a XYV grid to make a 2D plot. This would require that I take every X,Y pair that is the same and average the Voltage column under different Z values.
i.e
X Y. Z. V
1. 0. 1. 10
1. 0. 1.5. 5
1. 0. 0. 1
1. 1. 1. 1
Becomes:
X. Y. V.
1. 0. 5.333
1. 1. 1
How would I do this in python reading in a csv file either through pandas or csv?
答案1
得分: 1
IIUC,您可以尝试使用 groupby_mean
:
df.groupby(['X', 'Y'], as_index=False)['V'].mean()
X Y V
0 1.0 0.0 5.333333
1 1.0 1.0 1.000000
要读取CSV文件,使用 pd.read_csv
:
英文:
IIUC, you can try groupby_mean
:
>>> df.groupby(['X', 'Y'], as_index=False)['V'].mean()
X Y V
0 1.0 0.0 5.333333
1 1.0 1.0 1.000000
To read a csv file, use pd.read_csv
:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论