简体   繁体   中英

Fastest way of dropping zeros from a pandas series

I read in several worksheets of an excel file (> 15 MB) where each sheet has > 10000 columns. Sencondly I choose a single column (consists of only integers), drop all values == 0 from this column and write this column to a new df2. Additionally I calculate the descriptie statistics.

Data looks like this:

    Gel.Menge   Erf.datum      Freig.
0         0.0  26.11.2014  26.11.2014
1        10.0  06.11.2014  07.11.2014
2         5.0  19.12.2014  08.01.2015
3         7.0  07.07.2015  17.07.2015
4         0.0  21.07.2015  22.07.2015
5         5.0  18.03.2016  22.03.2016
6        10.0  29.03.2016  31.03.2016
7         0.0  20.07.2016  21.07.2016
8        20.0  13.10.2016  17.10.2016
9         0.0  01.12.2014  01.12.2014
10        0.0  20.04.2015  20.04.2015

The code I use is:

inpath=r"P:\Data.xlsx"

df1=pd.DataFrame()

for i in ["67059070","67059075","67060055","Screwing Total"]:
    df=pd.read_excel(io=inpath,header=0,sheetname="{0}".format(i))
    df1["Gel.Menge"]=df["Gel.Menge"].where(df["Gel.Menge"]!=0).dropna()
    print(np.round(df1.mode()))    
    print(np.round(df1.describe())

Unfortunately this code is super slow... is there a faster way to accomplish this?

Data taken from here and modified.

df

    Gel.Menge   Erf.datum      Freig.
0         0.0  26.11.2014  26.11.2014
1        10.0  06.11.2014  07.11.2014
2         5.0  19.12.2014  08.01.2015
3         7.0  07.07.2015  17.07.2015
4         0.0  21.07.2015  22.07.2015
5         5.0  18.03.2016  22.03.2016
6        10.0  29.03.2016  31.03.2016
7         0.0  20.07.2016  21.07.2016
8        20.0  13.10.2016  17.10.2016
9         0.0  01.12.2014  01.12.2014
10        0.0  20.04.2015  20.04.2015

Option 1
boolean indexing

df[df['Gel.Menge'] != 0]

   Gel.Menge   Erf.datum      Freig.
1       10.0  06.11.2014  07.11.2014
2        5.0  19.12.2014  08.01.2015
3        7.0  07.07.2015  17.07.2015
5        5.0  18.03.2016  22.03.2016
6       10.0  29.03.2016  31.03.2016
8       20.0  13.10.2016  17.10.2016

Option 2
np.where

m = np.where(df['Gel.Menge'], True, False)
m
array([False,  True,  True,  True, False,  True,  True, False,  True,
       False, False], dtype=bool)

df[m]

   Gel.Menge   Erf.datum      Freig.
1       10.0  06.11.2014  07.11.2014
2        5.0  19.12.2014  08.01.2015
3        7.0  07.07.2015  17.07.2015
5        5.0  18.03.2016  22.03.2016
6       10.0  29.03.2016  31.03.2016
8       20.0  13.10.2016  17.10.2016

Option 3
df.query

c = df['Gel.Menge'] 
df.query('@c != 0')

   Gel.Menge   Erf.datum      Freig.
1       10.0  06.11.2014  07.11.2014
2        5.0  19.12.2014  08.01.2015
3        7.0  07.07.2015  17.07.2015
5        5.0  18.03.2016  22.03.2016
6       10.0  29.03.2016  31.03.2016
8       20.0  13.10.2016  17.10.2016

Option 4
df.eval

df[df.eval('@c != 0')]

   Gel.Menge   Erf.datum      Freig.
1       10.0  06.11.2014  07.11.2014
2        5.0  19.12.2014  08.01.2015
3        7.0  07.07.2015  17.07.2015
5        5.0  18.03.2016  22.03.2016
6       10.0  29.03.2016  31.03.2016
8       20.0  13.10.2016  17.10.2016

Note : Two steps are necessary for query and eval due to the restrictions with handling column names.


Option 5
astype(bool)

df[df['Gel.Menge'].astype(bool)]

   Gel.Menge   Erf.datum      Freig.
1       10.0  06.11.2014  07.11.2014
2        5.0  19.12.2014  08.01.2015
3        7.0  07.07.2015  17.07.2015
5        5.0  18.03.2016  22.03.2016
6       10.0  29.03.2016  31.03.2016
8       20.0  13.10.2016  17.10.2016

Performance

print(df.shape)
(110000, 3)
100 loops, best of 3: 2.4 ms per loop
100 loops, best of 3: 2.36 ms per loop
100 loops, best of 3: 4.79 ms per loop
100 loops, best of 3: 4.97 ms per loop
100 loops, best of 3: 2.08 ms per loop

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