[英]Remove rows in dataframe only if a certain condition is met using Pandas
I have a dataframe where I would like to remove the rows from the ['Date'] column that contains.22 and.23 only if the energy column contains a value > 0.我有一个 dataframe ,我想从包含 22 和 23 的 ['Date'] 列中删除行,前提是能量列包含的值 > 0。
Data数据
ID Date type energy
AA Q1.22 ok 8
AA Q2.22 n 9
AA Q3.22 yes 8
AA Q1.23 ok 5
BB Q1.22 no 8
BB Q2.22 ok 8
BB Q3.22 0
BB Q1.23 0
Desired期望的
ID Date type energy
BB Q3.22 0
BB Q1.23 0
Doing正在做
df1 = df.drop(df[df.energy > 0].index) & df[df.Date.str.contains(".22|.23") == False]
However this is actually removing the rows that contain 0 ;然而,这实际上是删除包含 0 的行; However I wish to retain the rows that contain 0. I am still researching, any suggestion is appreciated
但是我希望保留包含 0 的行。我仍在研究中,欢迎提出任何建议
Use df.drop() to remove rows from DataFrame.使用 df.drop() 从 DataFrame 中删除行。
df.drop(df[(df["Date"].str.contains(pat=".22 |.23") == False) & (df["energy"] > 0)].index) df.drop(df[(df["Date"].str.contains(pat=".22 |.23") == False) & (df["energy"] > 0)].index)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.