數據分析師日常工作會涉及各種任務,比如數據預處理,數據分析,機器學習模型創建,模型部署。
在本文中,我將分享10個 Python 操作,它們可覆蓋90%的數據分析問題有所收獲點贊,收藏,關注
1,閱讀數據集
閱讀數據是數據分析的組成部分,了解如何從不同的文件格式讀取數據是數據分析師的第一步下面是如何使用 pandas 讀取包含 Covid—19 數據的 csv 文件的示例
import pandas as pd # reading the countries_data file along with the location within read_csv function.countries_df = pd.read_csv # showing the first 5 rows of the dataframe countries_df.head
以下是 countries_df.head 的輸出,我們可以使用它查看數據框的前 5 行:
2,匯總統計
下一步就是通過查看數據匯總來了解數據,例如 NewConfirmed,TotalConfirmed 等數字列的計數,均值,標準偏差,分位數以及國家代碼等分類列的頻率,最高出現值
countries_df.describe
使用 describe 函數,我們可以得到數據集連續變量的摘要,如下所示:
在 describe 函數中,我們可以設置參數"include = 'all'"來獲取連續變量和分類變量的摘要
countries_df.describe
3,數據選擇和過濾
分析其實不需要數據集的所有行和列,只需要選擇感興趣的列并根據問題過濾一些行。
例如,我們可以使用以下代碼選擇 Country 和 NewConfirmed 列:
countries_df)
我們還可以將數據過濾Country,使用 loc,我們可以根據一些值過濾列,如下所示:
countries_df.loc 'United States of America')
4,聚合
計數,總和,均值等數據聚合,是數據分析最常執行的任務之一。“我是武漢人,對那里的一草一木都很熟悉。”全市城市交通暫停運營,離漢通道暫時關閉,從前的熱鬧繁華不再。
我們可以使用聚合找到各國的 NewConfimed 病例總數使用 groupby 和 agg 函數執行聚合
countries_df.groupby).agg('NewConfirmed':'sum')5,Join
使用 Join 操作將 2 個數據集組合成一個數據集。”數據分析師連明告訴中工網記者。“然而疫情下的武漢市,一切變得完全陌生。
例如:一個數據集可能包含不同國家/地區的 Covid—19 病例數,另一個數據集可能包含不同國家/地區的緯度和經度信息。。
現在我們需要結合這兩個信息,那么我們可以執行如下所示的連接操作
countries_lat_lon = pd.read_excel# joining the 2 dataframe : countries_df and countries_lat_lon# syntax : pd.merge(left_df, right_df, on = 'on_column', how = 'type_of_join')joined_df = pd.merge(countries_df, countries_lat_lon, on = 'CountryCode', how = 'inner')joined_df6,內建函數
了解數學內建函數,如 min,max,mean,sum 等,對于執行不同的分析非常有幫助。
我們可以通過調用它們直接在數據幀上應用這些函數,這些函數可以在列上或在聚合函數中獨立使用,如下所示:
# finding sum of NewConfirmed cases of all the countries countries_df.sum# Output : 6,631,899# finding the sum of NewConfirmed cases across different countries countries_df.groupby(('Country')).agg('NewConfirmed':'sum')# Output # NewConfirmed#Country #Afghanistan 75#Albania 168#Algeria 247#Andorra 0#Angola 537,用戶自定義函數
我們自己編寫的函數是用戶自定義函數我們可以在需要時通過調用該函數來執行這些函數中的代碼
# User defined function is created using 'def' keyword, followed by function definition — 'addition'# and 2 arguments num1 and num2def addition(num1, num2): return num1+num2# calling the function using function name and providing the arguments print(addition(1,2))#output : 38,Pivot
Pivot 是將一列行內的唯一值轉換為多個新列,這是很棒的數據處理技術。“那種狀態下,那個時期,每一條數據都要特別清晰,準確。
在 Covid—19 數據集上使用 pivot_table 函數,我們可以將國家名稱轉換為單獨的新列:
# using pivot_table to convert values within the Country column into individual columns and # filling the values corresponding to these columns with numeric variable — NewConfimed pivot_df = pd.pivot_tablepivot_df9,遍歷數據框
很多時候需要遍歷數據框的索引和行,我們可以使用 iterrows 函數遍歷數據框:
# iterating over the index and row of a dataframe using iterrows function for index, row in countries_df.iterrows: print('Index is ' + str(index)) print('Country is '+ str(row('Country'))) # Output : # Index is 0# Country is Afghanistan# Index is 1# Country is Albania# .......10,字符串操作
很多時候我們處理數據集中的字符串列,在這種情況下,了解一些基本的字符串操作很重要。”和連明一起奮戰在武漢的分析師張順民說。
例如如何將字符串轉換為大寫,小寫以及如何找到字符串的長度。每一條數據都代表著一個人。
# country column to upper casecountries_df = countries_df('Country').str.upper# country column to lower casecountries_df('CountryCode_lower')=countries_df('CountryCode').str.lower# finding length of characters in the country column countries_df('len') = countries_df('Country').str.lencountries_df.head
。鄭重聲明:此文內容為本網站轉載企業宣傳資訊,目的在于傳播更多信息,與本站立場無關。僅供讀者參考,并請自行核實相關內容。
2023年2月15日,湯臣倍健與美團買藥在北京舉辦了2023
2021年收入增長目標應能確保實現,2022年收入增長預計將
具體來看,一是簡化業務流程和材料,便利北京冬奧會相關區域內境
炒股就看金麒麟分析師研報,權威,專業,及時,全面,助您挖掘潛
,據索尼官方消息,新星粉,銀河紫,星光藍,三款全新配色Dua
,新氧數據顏究院發布《2021醫美行業白皮書》,白皮書顯示,