AVt天堂网 手机版,亚洲va久久久噜噜噜久久4399,天天综合亚洲色在线精品,亚洲一级Av无码毛片久久精品

當前位置:首頁 > 科技  > 知識百科

十個 Python 小技巧,覆蓋了90%的數據分析需求!

來源: 責編: 時間:2023-08-07 16:30:00 238觀看
導讀 數據分析師日常工作會涉及各種任務,比如數據預處理、數據分析、機器學習模型創建、模型部署。在本文中,我將分享10個 Python 操作,它們可覆蓋90%的數據分析問題。有所收獲點贊

數據分析師日常工作會涉及各種任務,比如數據預處理、數據分析、機器學習模型創建、模型部署。ZQY28資訊網——每日最新資訊28at.com

在本文中,我將分享10個 Python 操作,它們可覆蓋90%的數據分析問題。有所收獲點贊、收藏、關注。ZQY28資訊網——每日最新資訊28at.com

1、閱讀數據集ZQY28資訊網——每日最新資訊28at.com

閱讀數據是數據分析的組成部分,了解如何從不同的文件格式讀取數據是數據分析師的第一步。下面是如何使用 pandas 讀取包含 Covid-19 數據的 csv 文件的示例。ZQY28資訊網——每日最新資訊28at.com

import pandas as pd ZQY28資訊網——每日最新資訊28at.com
# reading the countries_data file along with the location within read_csv function.ZQY28資訊網——每日最新資訊28at.com
countries_df = pd.read_csv('C:/Users/anmol/Desktop/Courses/Python for Data Science/Code/countries_data.csv') ZQY28資訊網——每日最新資訊28at.com
# showing the first 5 rows of the dataframe ZQY28資訊網——每日最新資訊28at.com
countries_df.head()ZQY28資訊網——每日最新資訊28at.com
ZQY28資訊網——每日最新資訊28at.com
ZQY28資訊網——每日最新資訊28at.com

以下是 countries_df.head() 的輸出,我們可以使用它查看數據框的前 5 行:ZQY28資訊網——每日最新資訊28at.com

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

2、匯總統計ZQY28資訊網——每日最新資訊28at.com

下一步就是通過查看數據匯總來了解數據,例如 NewConfirmed、TotalConfirmed 等數字列的計數、均值、標準偏差、分位數以及國家代碼等分類列的頻率、最高出現值ZQY28資訊網——每日最新資訊28at.com

countries_df.describe()ZQY28資訊網——每日最新資訊28at.com

使用 describe 函數,我們可以得到數據集連續變量的摘要,如下所示:ZQY28資訊網——每日最新資訊28at.com

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

在 describe() 函數中,我們可以設置參數"include = 'all'"來獲取連續變量和分類變量的摘要ZQY28資訊網——每日最新資訊28at.com

countries_df.describe(include = 'all')ZQY28資訊網——每日最新資訊28at.com

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

3、數據選擇和過濾ZQY28資訊網——每日最新資訊28at.com

分析其實不需要數據集的所有行和列,只需要選擇感興趣的列并根據問題過濾一些行。ZQY28資訊網——每日最新資訊28at.com

例如,我們可以使用以下代碼選擇 Country 和 NewConfirmed 列:ZQY28資訊網——每日最新資訊28at.com

countries_df[['Country','NewConfirmed']]ZQY28資訊網——每日最新資訊28at.com

我們還可以將數據過濾Country,使用 loc,我們可以根據一些值過濾列,如下所示:ZQY28資訊網——每日最新資訊28at.com

countries_df.loc[countries_df['Country'] == 'United States of America']ZQY28資訊網——每日最新資訊28at.com

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

4、聚合ZQY28資訊網——每日最新資訊28at.com

計數、總和、均值等數據聚合,是數據分析最常執行的任務之一。ZQY28資訊網——每日最新資訊28at.com

我們可以使用聚合找到各國的 NewConfimed 病例總數。使用 groupby 和 agg 函數執行聚合。ZQY28資訊網——每日最新資訊28at.com

countries_df.groupby(['Country']).agg({'NewConfirmed':'sum'})5、JoinZQY28資訊網——每日最新資訊28at.com

使用 Join 操作將 2 個數據集組合成一個數據集。ZQY28資訊網——每日最新資訊28at.com

例如:一個數據集可能包含不同國家/地區的 Covid-19 病例數,另一個數據集可能包含不同國家/地區的緯度和經度信息。ZQY28資訊網——每日最新資訊28at.com

現在我們需要結合這兩個信息,那么我們可以執行如下所示的連接操作ZQY28資訊網——每日最新資訊28at.com

countries_lat_lon = pd.read_excel('C:/Users/anmol/Desktop/Courses/Python for Data Science/Code/countries_lat_lon.xlsx')ZQY28資訊網——每日最新資訊28at.com
ZQY28資訊網——每日最新資訊28at.com
# joining the 2 dataframe : countries_df and countries_lat_lonZQY28資訊網——每日最新資訊28at.com
# syntax : pd.merge(left_df, right_df, on = 'on_column', how = 'type_of_join')ZQY28資訊網——每日最新資訊28at.com
joined_df = pd.merge(countries_df, countries_lat_lon, on = 'CountryCode', how = 'inner')ZQY28資訊網——每日最新資訊28at.com
joined_df6、內建函數ZQY28資訊網——每日最新資訊28at.com

了解數學內建函數,如 min()、max()、mean()、sum() 等,對于執行不同的分析非常有幫助。ZQY28資訊網——每日最新資訊28at.com

我們可以通過調用它們直接在數據幀上應用這些函數,這些函數可以在列上或在聚合函數中獨立使用,如下所示:ZQY28資訊網——每日最新資訊28at.com

# finding sum of NewConfirmed cases of all the countries ZQY28資訊網——每日最新資訊28at.com
countries_df['NewConfirmed'].sum()ZQY28資訊網——每日最新資訊28at.com
# Output : 6,631,899ZQY28資訊網——每日最新資訊28at.com
ZQY28資訊網——每日最新資訊28at.com
# finding the sum of NewConfirmed cases across different countries ZQY28資訊網——每日最新資訊28at.com
countries_df.groupby(['Country']).agg({'NewConfirmed':'sum'})ZQY28資訊網——每日最新資訊28at.com
ZQY28資訊網——每日最新資訊28at.com
# Output ZQY28資訊網——每日最新資訊28at.com
# NewConfirmedZQY28資訊網——每日最新資訊28at.com
#Country ZQY28資訊網——每日最新資訊28at.com
#Afghanistan 75ZQY28資訊網——每日最新資訊28at.com
#Albania 168ZQY28資訊網——每日最新資訊28at.com
#Algeria 247ZQY28資訊網——每日最新資訊28at.com
#Andorra 0ZQY28資訊網——每日最新資訊28at.com
#Angola 537、用戶自定義函數ZQY28資訊網——每日最新資訊28at.com

我們自己編寫的函數是用戶自定義函數。我們可以在需要時通過調用該函數來執行這些函數中的代碼。例如,我們可以創建一個函數來添加 2 個數字,如下所示:ZQY28資訊網——每日最新資訊28at.com

# User defined function is created using 'def' keyword, followed by function definition - 'addition()'ZQY28資訊網——每日最新資訊28at.com
# and 2 arguments num1 and num2ZQY28資訊網——每日最新資訊28at.com
def addition(num1, num2):ZQY28資訊網——每日最新資訊28at.com
return num1+num2ZQY28資訊網——每日最新資訊28at.com
ZQY28資訊網——每日最新資訊28at.com
# calling the function using function name and providing the arguments ZQY28資訊網——每日最新資訊28at.com
print(addition(1,2))ZQY28資訊網——每日最新資訊28at.com
#output : 38、PivotZQY28資訊網——每日最新資訊28at.com

Pivot 是將一列行內的唯一值轉換為多個新列,這是很棒的數據處理技術。ZQY28資訊網——每日最新資訊28at.com

在 Covid-19 數據集上使用 pivot_table() 函數,我們可以將國家名稱轉換為單獨的新列:ZQY28資訊網——每日最新資訊28at.com

# using pivot_table to convert values within the Country column into individual columns and ZQY28資訊網——每日最新資訊28at.com
# filling the values corresponding to these columns with numeric variable - NewConfimed ZQY28資訊網——每日最新資訊28at.com
pivot_df = pd.pivot_table(countries_df, columns = 'Country', values = 'NewConfirmed')ZQY28資訊網——每日最新資訊28at.com
pivot_df9、遍歷數據框ZQY28資訊網——每日最新資訊28at.com

很多時候需要遍歷數據框的索引和行,我們可以使用 iterrows 函數遍歷數據框:ZQY28資訊網——每日最新資訊28at.com

# iterating over the index and row of a dataframe using iterrows() function ZQY28資訊網——每日最新資訊28at.com
for index, row in countries_df.iterrows():ZQY28資訊網——每日最新資訊28at.com
print('Index is ' + str(index))ZQY28資訊網——每日最新資訊28at.com
print('Country is '+ str(row['Country']))ZQY28資訊網——每日最新資訊28at.com
ZQY28資訊網——每日最新資訊28at.com
# Output : ZQY28資訊網——每日最新資訊28at.com
# Index is 0ZQY28資訊網——每日最新資訊28at.com
# Country is AfghanistanZQY28資訊網——每日最新資訊28at.com
# Index is 1ZQY28資訊網——每日最新資訊28at.com
# Country is AlbaniaZQY28資訊網——每日最新資訊28at.com
# .......10、字符串操作ZQY28資訊網——每日最新資訊28at.com

很多時候我們處理數據集中的字符串列,在這種情況下,了解一些基本的字符串操作很重要。ZQY28資訊網——每日最新資訊28at.com

例如如何將字符串轉換為大寫、小寫以及如何找到字符串的長度。ZQY28資訊網——每日最新資訊28at.com

# country column to upper caseZQY28資訊網——每日最新資訊28at.com
countries_df['Country_upper'] = countries_df['Country'].str.upper()ZQY28資訊網——每日最新資訊28at.com
ZQY28資訊網——每日最新資訊28at.com
# country column to lower caseZQY28資訊網——每日最新資訊28at.com
countries_df['CountryCode_lower']=countries_df['CountryCode'].str.lower()ZQY28資訊網——每日最新資訊28at.com
ZQY28資訊網——每日最新資訊28at.com
# finding length of characters in the country column ZQY28資訊網——每日最新資訊28at.com
countries_df['len'] = countries_df['Country'].str.len()ZQY28資訊網——每日最新資訊28at.com
ZQY28資訊網——每日最新資訊28at.com
countries_df.head()ZQY28資訊網——每日最新資訊28at.com

本文鏈接:http://www.tebozhan.com/showinfo-119-2208-0.html十個 Python 小技巧,覆蓋了90%的數據分析需求!

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

上一篇: 云安全日報220308:Ubuntu Redis數據庫發現執行任意代碼漏洞,需要盡快升級

下一篇: 騙人還是文字強!MIT最新研究:DeepFake換臉還不如編輯動動筆

標簽:
  • 熱門焦點
Top