日韩成人免费在线_国产成人一二_精品国产免费人成电影在线观..._日本一区二区三区久久久久久久久不

當前位置:首頁 > 科技  > 軟件

Pandas中選擇和過濾數據的終極指南

來源: 責編: 時間:2023-11-30 17:30:30 233觀看
導讀Python pandas庫提供了幾種選擇和過濾數據的方法,如loc、iloc、[]括號操作符、query、isin、between等等本文將介紹使用pandas進行數據選擇和過濾的基本技術和函數。無論是需要提取特定的行或列,還是需要應用條件過濾,pa

Python pandas庫提供了幾種選擇和過濾數據的方法,如loc、iloc、[]括號操作符、query、isin、between等等hfx28資訊網——每日最新資訊28at.com

本文將介紹使用pandas進行數據選擇和過濾的基本技術和函數。無論是需要提取特定的行或列,還是需要應用條件過濾,pandas都可以滿足需求。hfx28資訊網——每日最新資訊28at.com

hfx28資訊網——每日最新資訊28at.com

選擇列

loc[]:根據標簽選擇行和列。df.row_label loc, column_label]hfx28資訊網——每日最新資訊28at.com

也可以使用loc進行切片操作:hfx28資訊網——每日最新資訊28at.com

df.loc['row1_label':'row2_label' , 'column1_label':'column2_label']hfx28資訊網——每日最新資訊28at.com

例如hfx28資訊網——每日最新資訊28at.com

# Using loc for label-based selection df.loc[:, 'Customer Country':'Customer State']

hfx28資訊網——每日最新資訊28at.com

# Using loc for label-based selection df.loc[[0,1,2], 'Customer Country':'Customer State']

hfx28資訊網——每日最新資訊28at.com

iloc[]:根據位置索引選擇行和列。df.iloc [row_position column_position]hfx28資訊網——每日最新資訊28at.com

可以使用iloc進行切片操作:hfx28資訊網——每日最新資訊28at.com

df.iloc['row1_position':'row2_position','col1_position':'col2_position']

例如:hfx28資訊網——每日最新資訊28at.com

# Using iloc for index-based selection df.iloc[[0,1,2,3] , [3,4,5,6,7,8]]  # or df.iloc[[0,1,2,3] , 3:9]

hfx28資訊網——每日最新資訊28at.com

# Using iloc for index-based selection df.iloc[:, 3:8]

hfx28資訊網——每日最新資訊28at.com

[]括號操作符:它允許選擇一個或多個列。df[['column_label']]或df[['column1', 'column2']]]hfx28資訊網——每日最新資訊28at.com

# Selecting a single column df[['Customer Country']]

hfx28資訊網——每日最新資訊28at.com

# Selecting multiple columns df[['Customer Country', 'Customer State']]

hfx28資訊網——每日最新資訊28at.com

過濾行

loc[]:按標簽過濾行。df.loc(條件)hfx28資訊網——每日最新資訊28at.com

# Using loc for filtering rows condition = df['Order Quantity'] > 3 df.loc[condition]  # or df.loc[df['Order Quantity'] > 3]

hfx28資訊網——每日最新資訊28at.com

# Using loc for filtering rows df.loc[df['Customer Country'] == 'United States']

hfx28資訊網——每日最新資訊28at.com

iloc():按位置索引篩選行。hfx28資訊網——每日最新資訊28at.com

# Using iloc for filtering rows df.iloc[[0, 2, 4]]

hfx28資訊網——每日最新資訊28at.com

# Using iloc for filtering rows df.iloc[:3, :2]

hfx28資訊網——每日最新資訊28at.com

[]括號操作符:它允許根據條件過濾行。df(條件)hfx28資訊網——每日最新資訊28at.com

# Using [] bracket operator for filtering rows# Using [] bracket operator for filtering rows condition = df['Order Quantity'] > 3 df[condition]  # or df[df['Order Quantity'] > 3]

hfx28資訊網——每日最新資訊28at.com

isin([]):基于列表過濾數據。df (df (column_name”).isin ([value1, ' value2 ']))hfx28資訊網——每日最新資訊28at.com

# Using isin for filtering rows df[df['Customer Country'].isin(['United States', 'Puerto Rico'])]

hfx28資訊網——每日最新資訊28at.com

# Filter rows based on values in a list and select spesific columns df[["Customer Id", "Order Region"]][df['Order Region'].isin(['Central America', 'Caribbean'])]

hfx28資訊網——每日最新資訊28at.com

# Using NOT isin for filtering rows df[~df['Customer Country'].isin(['United States'])]

hfx28資訊網——每日最新資訊28at.com

query():方法用于根據類似sql的條件表達式選擇數據。df.query(條件)hfx28資訊網——每日最新資訊28at.com

如果列名包含空格或特殊字符,首先應該使用rename()函數來重命名它們。hfx28資訊網——每日最新資訊28at.com

# Rename the columns before performing the query df.rename(columns={'Order Quantity' : 'Order_Quantity', "Customer Fname" : "Customer_Fname"}, inplace=True)  # Using query for filtering rows with a single condition df.query('Order_Quantity > 3')

hfx28資訊網——每日最新資訊28at.com

# Using query for filtering rows with multiple conditions df.query('Order_Quantity > 3 and Customer_Fname == "Mary"')

hfx28資訊網——每日最新資訊28at.com

between():根據在指定范圍內的值篩選行。df[df['column_name'].between(start, end)]hfx28資訊網——每日最新資訊28at.com

# Filter rows based on values within a range df[df['Order Quantity'].between(3, 5)]

hfx28資訊網——每日最新資訊28at.com

字符串方法:根據字符串匹配條件篩選行。例如str.startswith(), str.endswith(), str.contains()hfx28資訊網——每日最新資訊28at.com

# Using str.startswith() for filtering rows df[df['Category Name'].str.startswith('Cardio')]

hfx28資訊網——每日最新資訊28at.com

# Using str.contains() for filtering rows df[df['Customer Segment'].str.contains('Office')]

hfx28資訊網——每日最新資訊28at.com

更新值

loc[]:可以為DataFrame中的特定行和列并分配新值。hfx28資訊網——每日最新資訊28at.com

# Update values in a column based on a condition df.loc[df['Customer Country'] == 'United States', 'Customer Country'] = 'USA'

hfx28資訊網——每日最新資訊28at.com

iloc[]:也可以為DataFrame中的特定行和列并分配新值,但是他的條件是數字索引hfx28資訊網——每日最新資訊28at.com

# Update values in a column based on a condition df.iloc[df['Order Quantity'] > 3, 15] = 'greater than 3'  # condition = df['Order Quantity'] > 3 df.iloc[condition, 15] = 'greater than 3'

hfx28資訊網——每日最新資訊28at.com

replace():用新值替換DataFrame中的特定值。df.['column_name'].replace(old_value, new_value, inplace=True)hfx28資訊網——每日最新資訊28at.com

# Replace specific values in a column df['Order Quantity'].replace(5, 'equals 5', inplace=True)

hfx28資訊網——每日最新資訊28at.com

總結

Python pandas提供了很多的函數和技術來選擇和過濾DataFrame中的數據。比如我們常用的 loc和iloc,有很多人還不清楚這兩個的區別,其實它們很簡單,在Pandas中前面帶i的都是使用索引數值來訪問的,例如 loc和iloc,at和iat,它們訪問的效率是類似的,只不過是方法不一樣,我們這里在使用loc和iloc為例做一個簡單的說明:hfx28資訊網——每日最新資訊28at.com

loc:根據標簽(label)索引,什么是標簽呢?hfx28資訊網——每日最新資訊28at.com

行標簽就是我們所說的索引(index),列標簽就是列名(columns)hfx28資訊網——每日最新資訊28at.com

iloc,根據標簽的位置索引。hfx28資訊網——每日最新資訊28at.com

iloc就是 integer loc的縮寫。也就是說我們不知道列名的時候可以直接訪問的第幾行,第幾列hfx28資訊網——每日最新資訊28at.com

這樣解釋應該可以很好理解這兩個的區別了。最后如果你看以前(很久以前)的代碼可能還會看到ix,它是先于iloc、和loc的。但是現在基本上用iloc和loc已經完全能取代ix,所以ix已經被官方棄用了。如果有看到的話說明這個代碼已經很好了,并且完全可以使用iloc替代。hfx28資訊網——每日最新資訊28at.com

最后,通過靈活本文介紹的這些方法,可以更高效地處理和分析數據集,從而更好地理解和挖掘數據的潛在信息。希望這個指南能夠幫助你在數據科學的旅程中取得更大的成功!hfx28資訊網——每日最新資訊28at.com


hfx28資訊網——每日最新資訊28at.com

本文鏈接:http://www.www897cc.com/showinfo-26-35559-0.htmlPandas中選擇和過濾數據的終極指南

聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。郵件:2376512515@qq.com

上一篇: Python文件操作:高效處理文件的技巧

下一篇: 快速入門 Python sympy 庫:解決數學難題從此不再困擾!

標簽:
  • 熱門焦點
  • CSS單標簽實現轉轉logo

    轉轉品牌升級后更新了全新的Logo,今天我們用純CSS來實現轉轉的新Logo,為了有一定的挑戰性,這里我們只使用一個標簽實現,將最大化的使用CSS能力完成Logo的繪制與動畫效果。新logo
  • 不容錯過的MSBuild技巧,必備用法詳解和實踐指南

    一、MSBuild簡介MSBuild是一種基于XML的構建引擎,用于在.NET Framework和.NET Core應用程序中自動化構建過程。它是Visual Studio的構建引擎,可在命令行或其他構建工具中使用
  • 共享單車的故事講到哪了?

    來源丨??素斀浥c共享充電寶相差不多,共享單車已很久沒有被國內熱點新聞關照到了。除了一再漲價和用戶直呼用不起了。近日多家媒體再發報道稱,成都、天津、鄭州等地多個共享單
  • ESG的面子與里子

    來源 | 光子星球撰文 | 吳坤諺編輯 | 吳先之三伏大幕拉起,各地高溫預警不絕,但處于厄爾尼諾大“烤”之下的除了眾生,還有各大企業發布的ESG報告。ESG是“環境保
  • 疑似小米14外觀設計圖曝光:后置相機模組變化不大

    下半年的大幕已經開啟,而誰將成為下半年手機圈的主角就成為了大家關注的焦點,其中被傳有望拿下新一代驍龍8 Gen3旗艦芯片的小米14系列更是備受大家矚
  • 回歸OPPO兩年,一加贏了銷量,輸了品牌

    成為OPPO旗下主打性能的先鋒品牌后,一加屢創佳績。今年618期間,一加手機全渠道銷量同比增長362%,憑借一加 11、一加 Ace 2、一加 Ace 2V三款爆品,一加
  • 引領旗艦級影像能力向中端機普及 OPPO K11 系列發布 1799 元起

    7月25日,OPPO正式發布K系列新品—— OPPO K11 。此次 K11 在中端手機市場長期被忽視的影像板塊發力,突破性地搭載索尼 IMX890 旗艦大底主攝,支持 OIS
  • 朋友圈可以修改可見范圍了 蘋果用戶可率先體驗

    近日,iOS用戶迎來微信8.0.27正式版更新,除了可更換二維碼背景外,還新增了多項實用功能。在新版微信中,朋友圈終于可以修改可見范圍,簡單來說就是已發布的朋友圈
  • onebot M24巧系列一體機采用輕薄機身設計,現已在各平臺開售

    onebot M24 巧系列一體機目前已在線上線下各平臺同步開售。onebot M24 巧系列采用一體化輕薄機身設計,最薄處為 10.15mm,擁有寶石紅、午夜藍、石墨綠、雅致
Top 主站蜘蛛池模板: 巴彦淖尔市| 涪陵区| 电白县| 四平市| 姚安县| 镇安县| 汽车| 沙田区| 遵义县| 永泰县| 油尖旺区| 吴忠市| 博湖县| 鲁山县| 府谷县| 胶南市| 甘德县| 满城县| 鄱阳县| 会泽县| 双辽市| 津南区| 赣州市| 内黄县| 武川县| 海伦市| 锦州市| 张家口市| 龙江县| 平定县| 桃园市| 亚东县| 丹江口市| 方城县| 滕州市| 德保县| 肥东县| 阿瓦提县| 台湾省| 敖汉旗| 远安县|