Home > front end >  How to use python pandas to find a specific string in various rows
How to use python pandas to find a specific string in various rows

Time:01-21

I am trying to do my taxes. I have over 2,000 rows of data in a CSV of orders. I am trying to just count and print the rows that contain "CA" so I know the ones I need to pay sales tax on. I understand I can do this with python using the pandas library. I am stuck trying to get it to work though. Any help is appreciated.

CodePudding user response:

You can try this code below:

import pandas as pd

df = pd.read_csv(r'your csv file path')
res = df[(df.apply(lambda s: s.eq('CA').any(), axis=1))]
print(res)

CodePudding user response:

import pandas as pd


d = {'tax_info': ["CA", "CA", "CB", "CD", "CA"]}


df = pd.DataFrame(d)

taxable_rows = df[  df["tax_info"].str.contains("CA")]

print(taxable_rows)

Running the code above will yield

    tax_info
0         CA
1         CA
4         CA
  •  Tags:  
  • Related