0

I have dataframe df which contains a column check_r2 which contains values either True or False. I want to create another dataframe df1 which will contain rows for only those which have check_r2 as True

I tried with this code but getting empty dataframe as output, I am creating this dataframe from excel, where column values are in capital i.e TRUE or FALSE but when I Print them using print(df) I get True or False as output, I tried both ways while comparing but the output was same

code
df = read_data()
df1 = pd.DataFrame()
df1 = df[df['check_r2'] =="True"]

also tried

df1 = df1.append(df[df['check_r2'] =="True"])

enter image description here

1
  • Can you add the actual excel values? Commented Jan 2, 2021 at 13:38

1 Answer 1

1
df1 = df[df['check_r2'] ==True]

check the datatype

df.info()
check_r2    9 non-null bool

Pandas is converting it to boolean.

Sign up to request clarification or add additional context in comments.

3 Comments

this worked, but arent the values suppposed to be string
Pandas is converting it to boolean.>>>> yes, will keep this in mind, thank you.
Actually, when you are storing TRUE & FALSE in excel, it is stored as a logical value. Check by =TYPE(cell number), it will return 4 which shows it is a logical value. Excel stores additional value, unlike CSV. If you want to stop pandas from interpreting on their own you can use df = pandas.read_excel("temp.xlsx", dtype={"check_r2":object}) but, in this case it will not help because pandas will use excel own implementation which is also boolean. Try saving it to CSV and using df = pandas.read_csv("temp.xlsx", dtype={"check_r2":object}), then you will see TRUE, False.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.