简体   繁体   中英

Removing/Resampling pandas dataframe row based on condition

I have a pandas DataFrame representing some measurements the First Colum represent a continuous variable that has a very small increment 0.1 or 0.2 i need to resample this variable (and the whole DataFrame) to be every 1 increment

0     494.84284
1     494.86824
2     494.89364
3     494.91904
4     494.94444
5     494.96984
6     494.99524
7     495.02064
8     495.04604
9     495.07144
10    495.09684
11    495.12224
12    495.14764
13    495.17304
14    495.19844
15    495.22384
16    495.24924
17    495.27464
18    495.30004
19    495.32544
20    495.35084
21    495.37624
22    495.40164
23    495.42704
24    495.45244
25    495.47784
26    495.50324
27    495.52864
28    495.55404
29    495.57944

i tried to set this column as index and run the code below with no success

row_init = 0.0
for index, row in df.iterrows(): 
    if (index - row_init) < 1:
        #print (index)
        df.drop(index, inplace=True)
        row_init = index
        #print (row_init)

Example output:
0     494.84284
1     495.02064
2     496.47784
3     497.50324
4     498.52864
5     499.55404
6     500.57944

It looks like you just want the first value for each integer, so you can groupby the integer value and take the first!

df = pd.DataFrame({'data':[494.84284,494.86824,494.89364,494.91904,494.94444,494.96984,494.99524,495.02064,495.04604,495.07144,495.66072,496.01247,497.5000,497.9777,500.01354]})

df.groupby(df['data'].astype(int)).first().reset_index(drop=True)

Output

         data
0   494.84284
1   495.02064
2   496.01247
3   497.50000
4   500.01354

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM