pandas數(shù)據(jù)篩選和csv操作的實現(xiàn)方法
1. 數(shù)據(jù)篩選
a b c 0 0 2 4 1 6 8 10 2 12 14 16 3 18 20 22 4 24 26 28 5 30 32 34 6 36 38 40 7 42 44 46 8 48 50 52 9 54 56 58
(1)單條件篩選
df[df['a']>30] # 如果想篩選a列的取值大于30的記錄,但是之顯示滿足條件的b,c列的值可以這么寫 df[['b','c']][df['a']>30] # 使用isin函數(shù)根據(jù)特定值篩選記錄。篩選a值等于30或者54的記錄 df[df.a.isin([30, 54])]
(2)多條件篩選
可以使用&(并)與| (或)操作符或者特定的函數(shù)實現(xiàn)多條件篩選
# 使用&篩選a列的取值大于30,b列的取值大于40的記錄 df[(df['a'] > 30) & (df['b'] > 40)]
(3)索引篩選
a. 切片操作
df[行索引,列索引]或df[[列名1,列名2]]
#使用切片操作選擇特定的行 df[1:4] #傳入列名選擇特定的列 df[['a','c']]
b. loc函數(shù)
當每列已有column name時,用 df [ ‘a(chǎn)' ] 就能選取出一整列數(shù)據(jù)。如果你知道column names 和index,且兩者都很好輸入,可以選擇 .loc同時進行行列選擇。
In [28]: df.loc[0,'c'] Out[28]: 4 In [29]: df.loc[1:4,['a','c']] Out[29]: a c 1 6 10 2 12 16 3 18 22 4 24 28 In [30]: df.loc[[1,3,5],['a','c']] Out[30]: a c 1 6 10 3 18 22 5 30 34
c. iloc函數(shù)
如果column name太長,輸入不方便,或者index是一列時間序列,更不好輸入,那就可以選擇 .iloc了,該方法接受列名的index,iloc 使得我們可以對column使用slice(切片)的方法對數(shù)據(jù)進行選取。這邊的 i 我覺得代表index,比較好記點。
In [35]: df.iloc[0,2] Out[35]: 4 In [34]: df.iloc[1:4,[0,2]] Out[34]: a c 1 6 10 2 12 16 3 18 22 In [36]: df.iloc[[1,3,5],[0,2]] Out[36]: a c 1 6 10 3 18 22 5 30 34 In [38]: df.iloc[[1,3,5],0:2] Out[38]: a b 1 6 8 3 18 20 5 30 32
d. ix函數(shù)
ix的功能更加強大,參數(shù)既可以是索引,也可以是名稱,相當于,loc和iloc的合體。需要注意的是在使用的時候需要統(tǒng)一,在行選擇時同時出現(xiàn)索引和名稱, 同樣在同行選擇時同時出現(xiàn)索引和名稱。
df.ix[1:3,['a','b']] Out[41]: a b 1 6 8 2 12 14 3 18 20 In [42]: df.ix[[1,3,5],['a','b']] Out[42]: a b 1 6 8 3 18 20 5 30 32 In [45]: df.ix[[1,3,5],[0,2]] Out[45]: a c 1 6 10 3 18 22 5 30 34
e. at函數(shù)
根據(jù)指定行index及列l(wèi)abel,快速定位DataFrame的元素,選擇列時僅支持列名。
In [46]: df.at[3,'a'] Out[46]: 18
f. iat函數(shù)
與at的功能相同,只使用索引參數(shù)
In [49]: df.iat[3,0] Out[49]: 18
2. csv操作
csv文件內容
Supplier Name,Invoice Number,Part Number,Cost,Purchase Date Supplier X,001-1001,2341,$500.00 ,1/20/14 Supplier X,001-1001,2341,$500.00 ,1/20/14 Supplier X,001-1001,5467,$750.00 ,1/20/14 Supplier X,001-1001,5467,$750.00 ,1/20/14 Supplier Y,50-9501,7009,$250.00 ,1/30/14 Supplier Y,50-9501,7009,$250.00 ,1/30/14 Supplier Y,50-9505,6650,$125.00 ,2002/3/14 Supplier Y,50-9505,6650,$125.00 ,2002/3/14 Supplier Z,920-4803,3321,$615.00 ,2002/3/14 Supplier Z,920-4804,3321,$615.00 ,2002/10/14 Supplier Z,920-4805,3321,$615.00 ,2/17/14 Supplier Z,920-4806,3321,$615.00 ,2/24/14
(1)csv文件讀寫
關于read_csv函數(shù)中的參數(shù)說明參考博客:http://chabaoo.cn/article/164445.htm
import pandas as pd # 讀寫csv文件 df = pd.read_csv("supplier_data.csv") df.to_csv("supplier_data_write.csv",index=None)
(2)篩選特定的行
#Supplier Nmae列中姓名包含'Z',或者Cost列中的值大于600 print(df[df["Supplier Name"].str.contains('Z')]) print(df[df['Cost'].str.strip('$').astype(float) > 600]) print(df.loc[(df["Supplier Name"].str.contains('Z'))|(df['Cost'].str.strip('$').astype(float) > 600.0),:]) #行中的值屬于某個集合 li = [2341,6650] print(df[df['Part Number'].isin(li)]) print(df.loc[df['Part Number'].astype(int).isin(li),:]) #行中的值匹配某個模式 print(df[df['Invoice Number'].str.startswith("001-")])
(3)選取特定的列
#選取特定的列 #列索引值,打印1,3列 print(df.iloc[:,1:4:2]) #列標題打印 print(df.loc[:,["Invoice Number", "Part Number"]]) #選取連續(xù)的行 print(df.loc[1:4,:])
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
- pandas數(shù)據(jù)處理基礎之篩選指定行或者指定列的數(shù)據(jù)
- pandas按若干個列的組合條件篩選數(shù)據(jù)的方法
- pandas系列之DataFrame 行列數(shù)據(jù)篩選實例
- 使用pandas實現(xiàn)篩選出指定列值所對應的行
- 使用Pandas對數(shù)據(jù)進行篩選和排序的實現(xiàn)
- pandas 按日期范圍篩選數(shù)據(jù)的實現(xiàn)
- pandas按條件篩選數(shù)據(jù)的實現(xiàn)
- Pandas 如何篩選包含特定字符的列
- Python?Pandas條件篩選功能
- python使用pandas實現(xiàn)篩選功能方式
相關文章
python用matplotlib繪制二維坐標軸,設置箭頭指向,文本內容方式
這篇文章主要介紹了python用matplotlib繪制二維坐標軸,設置箭頭指向,文本內容方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-08-08利用Python實現(xiàn)Excel的文件間的數(shù)據(jù)匹配功能
這篇文章主要介紹了利用Python實現(xiàn)Excel的文件間的數(shù)據(jù)匹配,本文通過一個函數(shù)實現(xiàn)此功能,通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-06-06python數(shù)據(jù)挖掘使用Evidently創(chuàng)建機器學習模型儀表板
在本文中,我們將探索 Evidently 并創(chuàng)建交互式報告/儀表板。有需要的朋友歡迎大家收藏學習,希望能夠有所幫助,祝大家多多進步早日升職加薪2021-11-11