0

Iam working with dataset with year as the column name. Is there way to slice it.

here below is the example. I want to slice dataframe with customer count 0.

df.loc[df.2020==0,:] this yields error.

Department      2016    2017    2018    2019    2020
Electronics     2030    1029    1839    893     20
Babyshop        2030    1029    1839    893     0
Grocery         2030    1029    1839    893     2
Hardware        2030    1029    1839    893     10
rdas
  • 20,604
  • 6
  • 33
  • 46
gannu
  • 105
  • 1
  • 9

1 Answers1

0

Is this what you're looking for?

import pandas as pd
from io import StringIO

df = pd.read_csv(StringIO("""
Department      2016    2017    2018    2019    2020
Electronics     2030    1029    1839    893     20
Babyshop        2030    1029    1839    893     0
Grocery         2030    1029    1839    893     2
Hardware        2030    1029    1839    893     10
"""), sep='\s+')

print(df[df['2020'] == 0])

Output:

  Department  2016  2017  2018  2019  2020
1   Babyshop  2030  1029  1839   893     0

If you want to look for rows with 0 customers in more than 1 year, you can club them with an or condition like this:

print(df[(df['2019'] == 0) | (df['2020'] == 0)])

Here's a post with more on selecting rows based on multiple conditions.

Balaji Ambresh
  • 4,977
  • 2
  • 5
  • 17
  • 1
    This gives the fix, but fails to explain the error. You should say that `df.col` is a shortcut for `df['col']` that is only valid when `col` looks like an identifier (starts with a letter, does not contain spaces, ...), and does not clash with other attributes like `index`, `T`, etc. – Serge Ballesta Jun 10 '20 at 14:41
  • @ganeshghimire you now have the full code for reference – Balaji Ambresh Jun 10 '20 at 14:46
  • df2[df2.2020== 0.0] File "", line 1 df2[df2.2020== 0.0] ^ SyntaxError: invalid syntax – gannu Jun 10 '20 at 14:54
  • 2020 does not start with an alphabet – Balaji Ambresh Jun 10 '20 at 15:03