我想从.csv文件中按顺序提取特定值。 我的.csv文件如下所示:
t,px,py,pz,vx,vy
0.00040489,14.12025084,-0.002009773,0.000840943,0.195026080,1.355758297
0.00110044,12.83635303,-0.000548573,-0.00014433,0.130413970,-2.43868875
1.68165647,0.255617890,-0.001845726,-0.00084086,0.023879232,-8.39354209
1.68610220,0.255606449,-0.001963713,-0.00059876,0.023861204,-8.31501485
1.68900455,0.255587527,-0.002043956,-0.00044366,0.023849428,-8.26700131
4.99693312,0.229725068,0.0003837980,0.003745918,0.019379076,-3.09994830
5.00358073,0.229685715,0.0003664936,0.003709321,0.019376030,-3.11744550
5.00677838,0.229666449,0.0003580808,0.003691646,0.019374567,-3.12616105
5.01067399,0.229642709,0.0003477605,0.003670047,0.019372785,-3.13703933
9.99574307,0.237937739,-0.104983657,-0.08335162,0.018869346,-0.00283944
9.99935327,0.237987378,-0.105109574,-0.08342908,0.018871016,-0.00284325
10.0027278,0.238033837,-0.105227240,-0.08350144,0.018872581,-0.00284681
10.0060302,0.238079357,-0.105342358,-0.08357221,0.018874116,-0.00285029
10.0099914,0.238134027,-0.105480397,-0.08365702,0.018875962,-0.00285447
10.0142878,0.238193410,-0.105630066,-0.08374895,0.018877970,-0.00285901
.........
14.9980889,0.237862082,-0.054564047,-0.062497959,0.020898590,-0.0023804
15.0013937,0.237843713,-0.054438091,-0.062480914,0.020897489,-0.0023755
15.0056053,0.237820487,-0.054278111,-0.062459537,0.020896085,-0.0023693
15.0087124,0.237803480,-0.054160455,-0.062444016,0.020895048,-0.0023648
.................
从上面这个文件中,我想得到行数(列数为5,10等)。可被5整除且只有一个值的值就足够了。例如,一旦采用5,它应该寻找10,15等等。
我希望我的输出文件看起来像这样:
t,px,vx
5.00358073,0.229685715,0.019376030
10.0027278,0.238033837,0.018872581
15.0013937,0.237843713,0.020897489
答案 0 :(得分:1)
IIUC,我们在这里使用了新的'G'
df['G']=df.t//5
df.drop_duplicates('G',keep='first').loc[lambda x : x.G!=0]
Out[877]:
t px py pz vx vy G
6 5.003581 0.229686 0.000366 0.003709 0.019376 -3.117446 1.0
11 10.002728 0.238034 -0.105227 -0.083501 0.018873 -0.002847 2.0
16 15.001394 0.237844 -0.054438 -0.062481 0.020897 -0.002376 3.0
Adding `to_csv`
df.drop_duplicates('G',keep='first').loc[lambda x : x.G!=0].to_csv('your.csv')