Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 16951

iterate through a csv file using numpy

$
0
0

I have a CSV file with 4 columns with x, y positions for tracks.The 1st column is the ID of the track. The 2nd column is time. 3rd and 4th are X & Y co-ordinates.

Now the X & Y positions change with time (column#2) from 1 to 39. I have written functions (with for loops)that calculate Mean Square Displacement (MSD) for X and Y, as a function of time (column 2).

However, I want to create a 'for' loop in which the code can identify the ID# (from column 1), compute the above mentioned MSD for X & Y as function of time frame, for the first track. Then repeat this task for track#2 from column 1 and so on. Ultimately, write all of this data into a csv finally.

This would be a nested for loop where for every track# from col#1, it will compute MSD values.

Hope the problem statement is clear.

I am presenting data from ID#1 and ID#2 for simplification.

[ID Time    X   Y1   0   1601.335544 496.46088551   1   1581.838184 497.34409511   2   1574.051153 493.21399851   3   1559.965258 504.90568731   4   1559.163646 493.42458321   5   1552.987661 494.28480721   6   1548.432535 497.35843891   7   1538.938784 495.04172671   8   1531.85109  489.06657881   9   1513.806745 504.77040071   10  1510.326674 497.59948631   11  1504.674908 506.20319461   12  1501.314024 509.98077961   13  1486.108431 530.84057351   14  1478.028873 528.98721631   15  1461.280431 555.18103521   16  1457.827357 544.12382951   17  1438.153357 546.18961181   18  1432.669134 536.0799931   19  1421.693967 539.19763681   20  1420.047558 531.83607751   21  1407.484016 552.62129321   22  1401.323039 542.3875261   23  1392.214888 550.1672871   24  1383.369105 538.15342211   25  1376.908441 563.24086661   26  1375.965296 552.18777531   27  1356.164381 565.66036021   28  1350.494881 554.30329031   29  1343.425562 554.21450221   30  1338.147555 543.56547331   31  1319.226209 549.36670581   32  1315.186512 544.14236211   33  1304.524207 559.43541871   34  1307.192095 545.92071451   35  1297.79372  555.02784851   36  1293.623393 548.4954821   37  1272.472404 556.96436191   38  1267.257919 553.43172791   39  1256.48484  564.75478762   0   1938.04852  502.20962722   1   1910.274979 499.41212092   2   1905.602566 494.5641212   3   1892.023117 500.86742782   4   1890.41268  497.25684352   5   1889.370728 497.16787742   6   1879.222251 493.42593242   7   1854.213543 505.90901322   8   1849.009713 506.47187952   9   1827.907196 521.12497662   10  1825.557874 513.50557682   11  1811.655479 514.91857462   12  1804.375201 506.04952052   13  1790.033474 520.97514892   14  1782.406459 512.81877152   15  1768.6465   528.5228782   16  1767.990804 526.00582062   17  1761.603106 535.81459842   18  1753.677501 517.65323472   19  1747.538543 539.05092962   20  1746.84304  531.16239692   21  1742.643159 543.83484592   22  1736.949154 534.78015352   23  1729.319064 542.80457712   24  1724.696825 533.39675612   25  1704.024318 549.78687142   26  1692.312454 544.45823172   27  1686.267143 547.78292182   28  1673.865039 545.0915122   29  1662.986818 562.55775132   30  1656.270704 562.40029872   31  1642.961381 568.32869072   32  1643.388565 563.5122622   33  1631.965059 570.77826272   34  1634.874901 563.75638262   35  1622.105795 574.05279372   36  1616.78351  569.57726352   37  1611.596229 576.49762282   38  1608.866078 567.63588962   39  1591.533636 576.2326799]

This is the for loop I use for computing MSD from X & Y.

def calc_msd_np(x):    msd = []    for s in range(1,len(x)):        dx = x[s:] - x[:-s]        msd.append(np.average(dx**2))    return msdprint(calc_msd_np(x))

Similarly, I use the for loop for Y-coordinate. Basically, this MSD for X & Y needs to be iterated for each ID.

I am very new to this so figuring out nested loops is very challenging.


Viewing all articles
Browse latest Browse all 16951

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>