客户需求
查看销售人员不为空值的行
数据存储情况如图:
代码实现
import pandas as pd data = pd.read_excel('test.xlsx',sheet_name='Sheet1') datanota = data[data['销售人员'].notna()] print(datanota)
输出结果
D:\Python\Anaconda\python.exe D:/Python/test/EASdeal/test.py
城市 销售金额 销售人员
0 北京 10000 张丽丽
1 上海 50000 潇潇
2 深圳 60000 笨笨笨
3 成都 40000 达达Process finished with exit code 0
如何删除特定列为空/ NaN的行?
我有一个csv文件.我读了它:
import pandas as pd data = pd.read_csv('my_data.csv', sep=',') data.head()
它的输出如下:
id city department sms category
01 khi revenue NaN 0
02 lhr revenue good 1
03 lhr revenue NaN 0
我想删除sms列为空/ NaN的所有行.什么是有效的方法呢?
解决方法:
将dropna与参数子集一起使用以指定用于检查NaN的列:
data = data.dropna(subset=['sms']) print (data) id city department sms category 1 2 lhr revenue good 1
boolean indexing和notnull的另一个解决方案:
data = data[data['sms'].notnull()] print (data) id city department sms category 1 2 lhr revenue good 1
替代query:
print (data.query("sms == sms")) id city department sms category 1 2 lhr revenue good 1
计时
#[300000 rows x 5 columns] data = pd.concat([data]*100000).reset_index(drop=True) In [123]: %timeit (data.dropna(subset=['sms'])) 100 loops, best of 3: 19.5 ms per loop In [124]: %timeit (data[data['sms'].notnull()]) 100 loops, best of 3: 13.8 ms per loop In [125]: %timeit (data.query("sms == sms")) 10 loops, best of 3: 23.6 ms per loop