Calculation in pandas dataframe using loop

Viewed 50

I want to perform desired computations based in my 'x' and 'y' coordinate values as in below table (Table 1):

TIMESTEP    nparticles      v_x     v_y     radius      area_sum        vx_a1   vy_a1   phi_1
0       1       0.0     0.0     0.490244    0.7550478008000959          1.90579     -1.83605            0.36630
100     1   0.369944    0.196252    0.490244    0.7550478008000959
200     1   -0.110178   -0.233131   0.490244    0.7550478008000959
...
...
97400   1   -1.03617    -7.24768    0.461981    0.6704989496863082
97500   1   -1.30016    -7.25768    0.461981    0.6704989496863082
...
...

For which I am using this code for my above generated dataframe:

bindistance = 0.25
orfl = -4.0
orfr = 4.0
bin_xc = np.arange(orfl, orfr, bindistance)
nbins = len(bin_xc)

binx = 0.25
xo_min = -4.0
xo_max = 4.0
xb1_c = xo_min
xb1_max = xb1_c + (binx * 2)
xb1_min = xb1_c - (binx * 2)
yb_min = -0.5
yb_max = 0.5
yb_c = 0
x_particle1 = df.loc[(df['x'] < xb1_max) & 
(df['x'] > xb1_min)]
xy_particle1 = 
x_particle1.loc[(x_particle1['y'] < yb_max) 
& (x_particle1['y'] > yb_min)]
output1 = xy_particle1.groupby("TIMESTEP").agg(nparticles = ("id", "count"), v_x=("vx", "sum"), v_y=("vy", "sum"), radius = ("radius", "sum"), area_sum = ("Area", "sum"))
nsum1 = output1["nparticles"].sum()
vxsum1 = output1["v_x"].sum()
vysum1 = output1["v_y"].sum()
v_a1 = vxsum1 / nsum1
vy_a1 = vysum1 / nsum1
phi_1 = output1["area_sum"].sum() / 1001

But I am having very large number of desired dataframes (first dataframe is above shown) based in my 'x' and 'y' coordinate conditions. So manually writing each code 50 or more times is not feasible. How I can do with using a loop or otherwise? Please help

This is my input dataset (df):

TIMESTEP    id  radius  x   y   vx  vy  Area
0   42  0.490244    -3.85683    0.489375    0.0     0.0     0.7550478008000959
0   245 0.479994    -2.88838    0.479446    0.0     0.0     0.7238048519265009
0   344 0.463757    -1.94613    0.463363    0.0     0.0     0.6756640757454175
0   313 0.503268    -0.981364   0.501991    0.0     0.0     0.7956984398459999
...
...
100000  1051 0.542993   0.887743    1.71649 -0.309668   -5.83282    0.9262715700848821
100000  504 0.540275    2.87158 1.94939 -5.76545    -2.30889    0.9170217083878441
100000  589 0.450005    3.86868 1.89373 -4.49676    -2.63977    0.636186649597414
...
...
0 Answers
Related