[英]How to remove values that do not meet condition from pandas data frame column?
如何從此數據集中刪除年齡不足的行?
原始數據集
ID age
xqd26543231 12
xqd22222231 29
xqd88823231 64
我試過什么?
#remove under age users
eligibleAge = myDataFrame['Age']>18
myDataFrame['Age'] = myDataFrame[eligibleAge]
意外的結果
ID age
xqd26543231 xqd26543231
xqd22222231 xqd22222231
xqd88823231 xqd88823231
我的期望
ID age
xqd22222231 29
xqd88823231 64
我認為你可以這樣做myDataFrame = myDataFrame[myDataFrame['Age']>18]
在您的代碼中, myDataFrame['Age'] = myDataFrame[eligibleAge]
右側的長度與您分配的左側尺寸不同,並且您正在將 dataframe 分配給系列。
您可以在此處使用df.query
。
df.query('age>18')
ID age
1 xqd22222231 29
2 xqd88823231 64
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.