'Can't apply conditions and select colums in pandas dataframe
Problem Context:
I connected to a database usingmysql.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 thepd.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.
Solution 1:[1]
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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Maximilian Press |
