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

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

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

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

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

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

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

選擇列

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

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

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

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

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

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

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

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

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

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

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

例如:hOK28資訊網——每日最新資訊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]

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

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

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

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

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

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

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

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

過濾行

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

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

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

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

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

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

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

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

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

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

[]括號操作符:它允許根據條件過濾行。df(條件)hOK28資訊網——每日最新資訊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]

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

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

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

hOK28資訊網——每日最新資訊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'])]

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

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

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

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

如果列名包含空格或特殊字符,首先應該使用rename()函數來重命名它們。hOK28資訊網——每日最新資訊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')

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

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

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

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

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

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

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

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

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

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

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

更新值

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

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

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

iloc[]:也可以為DataFrame中的特定行和列并分配新值,但是他的條件是數字索引hOK28資訊網——每日最新資訊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'

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

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

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

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

總結

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

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

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

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

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

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

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


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

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

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

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

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

標簽:
  • 熱門焦點
Top 主站蜘蛛池模板: 常宁市| 都兰县| 定南县| 淮阳县| 阿坝| 阿尔山市| 齐齐哈尔市| 拉萨市| 克拉玛依市| 巧家县| 垫江县| 通河县| 祁连县| 东至县| 富源县| 庄河市| 泸溪县| 东兰县| 东丽区| 瑞安市| 昭平县| 夏邑县| 余姚市| 大宁县| 湖州市| 闸北区| 开原市| 富裕县| 邮箱| 台北县| 新兴县| 吴川市| 隆尧县| 买车| 舞钢市| 呼玛县| 二手房| 贵溪市| 博湖县| 山西省| 武平县|