Python實(shí)現(xiàn)刪除某列中含有空值的行的示例代碼
客戶需求
查看銷售人員不為空值的行
數(shù)據(jù)存儲(chǔ)情況如圖:

代碼實(shí)現(xiàn)
import pandas as pd
data = pd.read_excel('test.xlsx',sheet_name='Sheet1')
datanota = data[data['銷售人員'].notna()]
print(datanota)
輸出結(jié)果
D:\Python\Anaconda\python.exe D:/Python/test/EASdeal/test.py
城市 銷售金額 銷售人員
0 北京 10000 張麗麗
1 上海 50000 瀟瀟
2 深圳 60000 笨笨笨
3 成都 40000 達(dá)達(dá)Process finished with exit code 0
如何刪除特定列為空/ NaN的行?
我有一個(gè)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與參數(shù)子集一起使用以指定用于檢查NaN的列:
data = data.dropna(subset=['sms']) print (data) id city department sms category 1 2 lhr revenue good 1
boolean indexing和notnull的另一個(gè)解決方案:
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
計(jì)時(shí)
#[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
到此這篇關(guān)于Python實(shí)現(xiàn)刪除某列中含有空值的行的示例代碼的文章就介紹到這了,更多相關(guān)Python刪除某列空值內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
BatchNorm2d原理、作用及pytorch中BatchNorm2d函數(shù)的參數(shù)使用
這篇文章主要介紹了BatchNorm2d原理、作用及pytorch中BatchNorm2d函數(shù)的參數(shù)使用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-12-12
python中的關(guān)鍵字參數(shù)*args和**kwargs詳解
這篇文章主要介紹了python中的關(guān)鍵字參數(shù)*args和**kwargs詳解,在定義類或函數(shù)時(shí),有時(shí)候會(huì)用到*args和**kwargs,前者叫位置參數(shù),后者叫關(guān)鍵字參數(shù),需要的朋友可以參考下2023-11-11
Python實(shí)現(xiàn)讀取目錄所有文件的文件名并保存到txt文件代碼
這篇文章主要介紹了Python實(shí)現(xiàn)讀取目錄所有文件的文件名并保存到txt文件代碼,本文分別使用os.listdir和os.walk實(shí)現(xiàn)給出兩段實(shí)現(xiàn)代碼,需要的朋友可以參考下2014-11-11
python+opencv實(shí)現(xiàn)視頻抽幀示例代碼
下面是采用以幀數(shù)為間隔的方法進(jìn)行視頻抽幀,為了避免不符合項(xiàng)目要求的數(shù)據(jù)增強(qiáng),博主要求技術(shù)人員在錄制視頻時(shí)最大程度地讓攝像頭進(jìn)行移動(dòng)、旋轉(zhuǎn)以及遠(yuǎn)近調(diào)節(jié)等,對(duì)python opencv視頻抽幀示例代碼感興趣的朋友一起看看吧2021-06-06
Keras實(shí)現(xiàn)Vision?Transformer?VIT模型示例詳解
這篇文章主要為大家介紹了Keras實(shí)現(xiàn)Vision?Transformer?VIT模型示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05
Python導(dǎo)出數(shù)據(jù)到Excel可讀取的CSV文件的方法
這篇文章主要介紹了Python導(dǎo)出數(shù)據(jù)到Excel可讀取的CSV文件的方法,設(shè)計(jì)Python操作Excel的相關(guān)技巧,需要的朋友可以參考下2015-05-05
用python給csv里的數(shù)據(jù)排序的具體代碼
在本文里小編給大家分享的是關(guān)于用python給csv里的數(shù)據(jù)排序的具體代碼內(nèi)容,需要的朋友們可以學(xué)習(xí)下。2020-07-07

