Python小点dian儿: 按列条件筛选、删除DataFrame的行
程序员文章站
2022-07-12 14:11:32
...
遇到清洗数据的问题,需要把某一列中为指定元素的数据,整行去除
尝试了drop却不能到达理想的效果,
isin效果理想。
import pandas as pd
df = pd.DataFrame({"key":['green','red', 'blue'],
"data1":['a','b','c'],"sorce": [33,61,99]})
data1 key sorce
0 a green 33
1 b red 61
2 c blue 99
mport pandas as pd
df = pd.DataFrame({"key":['green','red', 'blue'],
"data1":['a','b','c'],"sorce": [33,61,99]})
data1 = pd.concat([df,df],ignore_index=True)
data2=data1[-data1.sorce.isin([61])]
print("---------------")
print(data1)
print("---------------")
print(data2)
print("---------------")
data3=data1.drop(data1.ix[:,'sorce']==61)
print(data3)
---------------
data1 key sorce
0 a green 33
1 b red 61
2 c blue 99
3 a green 33
4 b red 61
5 c blue 99
---------------
data1 key sorce
0 a green 33
2 c blue 99
3 a green 33
5 c blue 99
---------------
data1 key sorce
2 c blue 99
3 a green 33
4 b red 61
5 c blue 99
Process finished with exit code 0
data.name.isin([筛选元素])
对dataframe的某列(name为列名)进行筛选,加负号的原因是想删除符合条件的行,不写负号是筛选出符合条件的行