Home > Enterprise >  filter out values from dictionary and add to df
filter out values from dictionary and add to df

Time:10-05

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.

CodePudding user response:

Try:

df_merged = df[df['art'].isin(['R','P'])]['abc']

CodePudding user response:

I seem to be able to filter out what you want just fine:

import pandas as pd
df = {}
df["abc"] = pd.DataFrame({"art": ["A", "B", "C", "P", "R", "S"], "bert": [1, 2, 3, 4, 5, 6]})
df_merged = df["abc"][df["abc"]["art"].isin(["R", "P"])]
df_merged
#{'abc':   art  bert
#3   P     4
#4   R     5}

df_merged shows a Series[_bool] type instead of Dataframe.

Should be correct, since the pandas.DataFrame.isin() method returns a bool array whose rows satisfy the condition, so you can easily filter using that:

df["abc"]["art"].isin(["R", "P"])
#3    True
#4    True
#Name: art, dtype: bool
  • Related