bibscy
bibscy

Reputation: 2708

How to remove values that do not meet condition from pandas data frame column?

How can I remove under age rows from this data set?

Original dataset

ID                 age
xqd26543231        12
xqd22222231        29
xqd88823231        64

What I tried?

#remove under age users
eligibleAge = myDataFrame['Age']>18
myDataFrame['Age'] = myDataFrame[eligibleAge]

Unexpected result

ID                 age
xqd26543231        xqd26543231
xqd22222231        xqd22222231
xqd88823231        xqd88823231

What I expect

ID                 age
xqd22222231        29
xqd88823231        64

Upvotes: 2

Views: 60

Answers (2)

Ch3steR
Ch3steR

Reputation: 20669

You can use df.query here.

df.query('age>18')
            ID  age
1  xqd22222231   29
2  xqd88823231   64

Upvotes: 0

Sam
Sam

Reputation: 515

I think you can just do myDataFrame = myDataFrame[myDataFrame['Age']>18]

In your code, myDataFrame['Age'] = myDataFrame[eligibleAge] right side has different length as left size you are assigning to, and also you are assigning a dataframe onto a serie.

Upvotes: 1

Related Questions