Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Find row whose value is always zero pandas

I have multiple table like –

Table A –

Seller Date sales
Ron 1-Aug 0
Jon 1-Aug 2
Kon 1-Aug 3
Kat 1-Aug 2
Mat 1-Aug 4

Table B –

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Seller Date sales
Ron 2-Aug 0
Jon 2-Aug 2
Kon 2-Aug 3
Kat 2-Aug 0
Mat 2-Aug 4

Table C –

Seller Date sales
Ron 3-Aug 0
Jon 3-Aug 2
Kon 3-Aug 3
Kat 3-Aug 0
Mat 3-Aug 4

Table D –

Seller Date sales
Ron 4-Aug 0
Jon 4-Aug 2
Kon 4-Aug 3
Kat 4-Aug 0
Mat 4-Aug 4

I need to find the seller list whose value is always zero like –

Ron – Always zero

Another finding is – Find the seller whose value is zero in last 3 dates like –

Kat – Zero in the last 3 dates. In Table A for date 1-Aug Kat had some value.

Create a list of sellers like that. I have many data in similar types of tables.

Note – Tables are not in order by dates.

>Solution :

First is necessary create DataFrame with converting columns to datetimes and sorting:

L = [df1, df2, df3, df4]

df=pd.concat([x.set_index('Seller')['sales'].rename(x['Date'].iat[0]) for x in L], axis=1)
df.columns = pd.to_datetime('2023'+df.columns, format='%Y%d-%b')

df = df.sort_index(axis=1)
print (df)
        2023-08-01  2023-08-02  2023-08-03  2023-08-04
Seller                                                
Ron              0           0           0           0
Jon              2           2           2           2
Kon              3           3           3           3
Kat              2           0           0           0
Mat              4           4           4           4

Then test 0 values and get list of names from indices if all values match:

m = df.eq(0)

out1 = df.index[m.all(axis=1)].tolist()
print (out1)
['Ron']

And for test only last 3 zero values:

N = 3
out2 = df.index[m.iloc[:, -N:].all(axis=1)].difference(out1).tolist()
print (out2)
['Kat']
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading