Search code examples
python-3.xpost-processing

How to read data from a txt file into rows and columns for avergae calculatins Python


So I'm using PIV (openPiv) to process images. After processing and saving the data I get a .txt file with three columns x y V:

x: 44 88 132 44 88 132

y: 100 100 100 50 50 50

V: 5 0 2 3 6 7

I need to draw a graph of x vs V but for the same value of x I have 2 values of V so I want to average over 2. I am therfore trying to write a code in python that can read the value of x in the file and bring back all the related V values so i can sum and average them. Let's say it reads x=44, goes to the file and se that for x=44, v=5 and 3.

Help would be much appriciated Thank you very much


Solution

  • import pandas as pd
    file=pd.read_csv('file.csv', sep='\t')
    velocity_along_x=file.groupby('x')['V'].mean()
    velocity_along_x.plot()
    

    I found a simpler way using Pandas