I have a dictionary where I read an sql table:
df = {}
df['abc'] = pd.read_sql_table('abc', engine, schema_z2,
columns = cols)
Now, I want to filter out data such that only those rows with values "R" and "P" from the art
column are kept. This is what I tried after reading this code snippet somewhere.
df_merged = df['abc'][df['abc']['art'].isin(['R','P'])]
print(df_merged)
When I hover over df_merged
in Visual Studio Code, it says that it is a dataframe, which is what I wanted. However, at the end when I run my code,df_merged
is empty, even though it should have rows. I could be using a wrong syntax here: df['abc'][df['abc']['art'].isin(['R','P'])]
but I am unable to identify how to change it.
A similar question How to filter Pandas dataframe using 'in' and 'not in' like in SQL
does not help because I am already using isin()
and I am trying to filter values from a dictionary not a df initially.
and if I just do this:
df_merged =df['abc']['art_kennz'].isin(['R','P','SP','GP'])
df_merged
shows a Series[_bool] type instead of Dataframe.
Edit:
I tried this with the following test data https://docs.google.com/spreadsheets/d/1cykNjViW_DacwWZNaIHWEh3E8OqxsVon/edit?usp=sharing&ouid=115380043465372211112&rtpof=true&sd=true:
import pandas as pd
df = {}
df['abc'] = pd.read_excel('./testing.xlsx')
print(df)
df_merged = df['abc'][df['abc']['art_kennz'].isin(['S','P','SP','GP'])]
df_merged.head()
and I get an empty dataset upon printing, which shouldn't be the case