Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to select all the columnar values from only the first 20 unique dates (whose instance counts are not equal) in Pandas

In my dataset, each date appears several times with different hourly values for distance. The number of times each date appears is not the same. I want to select all the columns for only the first and last 20 unique dates in Pandas. Please help me to figure it out.

Here is my dataset:

 Distance        Date_x
0      0.089278   3/27
1      0.000275   3/27
2     21.497275   3/27
3     13.820573   3/27
4      0.000000   3/27
5      0.000000   3/27
6      0.000000   3/27
7     14.648255   3/27
8      0.389053   3/27
9      0.007374   3/27
10     0.432203   3/27
11     1.007316   3/27
12     0.678015   3/27
13     0.028617   3/27
14     0.373197   3/27
15     0.873783   3/27
16     0.043098   3/27
17     0.045681   3/27
18     1.240625   3/27
19    10.180238   3/27
20     3.267828   3/28
21     0.000774   3/28
22    35.317742   3/28
23     0.000000   3/28
24    13.820617   3/28
25    13.820617   3/28
26    20.732181   3/28
27    20.732181   3/28
28    41.461852   3/28
29     0.000000   3/28
...         ...    ...
1290   0.276866   5/30
1291   0.240057   5/30
1292   0.320576   5/30
1293   0.808575   5/30
1294   0.393489   5/30
1295   7.924056   5/31
1296   0.000177   5/31
1297   0.000000   5/31
1298   0.041471   5/31
1299   0.000000   5/31
1300   0.000000   5/31
1301   0.000000   5/31
1302   0.000000   5/31
1303   0.000000   5/31
1304   0.000000   5/31
1305   0.013843   5/31
1306   0.064182   5/31
1307   9.012817   5/31
1308  12.984884   5/31
1309   3.362251   5/31
1310   4.595614   5/31
1311   3.122168   5/31
1312  39.019543   5/31
1313  15.392655   5/31
1314  24.017332   5/31
1315  13.079625   5/31
1316  54.371674   5/31
1317  65.251377   5/31
1318  51.051755    6/1
1319  23.214170    6/1

[1320 rows x 2 columns]
like image 316
Katrina Avatar asked Nov 25 '25 00:11

Katrina


1 Answers

using unique and np.r_ with isin

df[df.Date_x.isin(df.Date_x.unique()[np.r_[0:20,-20:0]])]
like image 117
BENY Avatar answered Nov 27 '25 12:11

BENY



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!