Can't apply conditions and select colums in pandas dataframe

Problem Context:

I connected to a database using mysql.connector and tried printing the dataframe. It worked perfectly fine. Now when I try to apply conditions on that very dataframe (which is now stored inside a variable) it generates an empty table.

What I want to do:

Just like in a sql query where we can define both things(**columns** to display and **conditions** to follow) in one query. I want to do it using pandas, but without using the pd.read_sql(). I want to do it using pandas functions only.

Code:

# importing dataframe from database
#this is just to show that a table is stored inside "df" variable    
df = DatabaseTable 

new_df = df.loc[(df["month"] == "JAN") &
    (df["topic_number"] >= 5) ,
    ["name","topic_number","month"]]
print(new_df)

OUTPUT>>

--------------------------------------
name   |   topic_number   |   month
--------------------------------------
empty  |   empty          |   empty
       |                  |   

What am I doing wrong here? I followed Pandas Docs but didn't get anywhere. There are no exceptions thrown. It just shows me this empty table, whereas if I print df it shows me completely populated table.

Upvotes: 0

Views: 39

Answers (1)

Maximilian Press
Maximilian Press

Reputation: 363

Your code works for me when I create an artificial example.

I would suggest looking at your data in more detail to ensure that they are what you think they are, posting a sample might be helpful.

import pandas as pd
df = pd.DataFrame()
df["month"] = pd.Series(["JAN", "JAN", "FEB"])
df["name"] = pd.Series(["max", "muhammad", "jane"])
df["topic_number"] = pd.Series([10, 1, 10])

new_df = df.loc[(df["month"] == "JAN") &
     (df["topic_number"] >= 5) ,
     ["name","topic_number","month"]]

print(new_df)

This script prints:

  name  topic_number month
0  max            10   JAN

Upvotes: 1

Related Questions