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

當(dāng)前位置:首頁 > 科技  > 知識(shí)百科

十個(gè) Python 小技巧,覆蓋了90%的數(shù)據(jù)分析需求!

來源: 責(zé)編: 時(shí)間:2023-08-07 16:30:00 212觀看
導(dǎo)讀 數(shù)據(jù)分析師日常工作會(huì)涉及各種任務(wù),比如數(shù)據(jù)預(yù)處理、數(shù)據(jù)分析、機(jī)器學(xué)習(xí)模型創(chuàng)建、模型部署。在本文中,我將分享10個(gè) Python 操作,它們可覆蓋90%的數(shù)據(jù)分析問題。有所收獲點(diǎn)贊

數(shù)據(jù)分析師日常工作會(huì)涉及各種任務(wù),比如數(shù)據(jù)預(yù)處理、數(shù)據(jù)分析、機(jī)器學(xué)習(xí)模型創(chuàng)建、模型部署。K2f28資訊網(wǎng)——每日最新資訊28at.com

在本文中,我將分享10個(gè) Python 操作,它們可覆蓋90%的數(shù)據(jù)分析問題。有所收獲點(diǎn)贊、收藏、關(guān)注。K2f28資訊網(wǎng)——每日最新資訊28at.com

1、閱讀數(shù)據(jù)集K2f28資訊網(wǎng)——每日最新資訊28at.com

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

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

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

K2f28資訊網(wǎng)——每日最新資訊28at.com

2、匯總統(tǒng)計(jì)K2f28資訊網(wǎng)——每日最新資訊28at.com

下一步就是通過查看數(shù)據(jù)匯總來了解數(shù)據(jù),例如 NewConfirmed、TotalConfirmed 等數(shù)字列的計(jì)數(shù)、均值、標(biāo)準(zhǔn)偏差、分位數(shù)以及國家代碼等分類列的頻率、最高出現(xiàn)值K2f28資訊網(wǎng)——每日最新資訊28at.com

countries_df.describe()K2f28資訊網(wǎng)——每日最新資訊28at.com

使用 describe 函數(shù),我們可以得到數(shù)據(jù)集連續(xù)變量的摘要,如下所示:K2f28資訊網(wǎng)——每日最新資訊28at.com

K2f28資訊網(wǎng)——每日最新資訊28at.com

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

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

K2f28資訊網(wǎng)——每日最新資訊28at.com

3、數(shù)據(jù)選擇和過濾K2f28資訊網(wǎng)——每日最新資訊28at.com

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

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

countries_df[['Country','NewConfirmed']]K2f28資訊網(wǎng)——每日最新資訊28at.com

我們還可以將數(shù)據(jù)過濾Country,使用 loc,我們可以根據(jù)一些值過濾列,如下所示:K2f28資訊網(wǎng)——每日最新資訊28at.com

countries_df.loc[countries_df['Country'] == 'United States of America']K2f28資訊網(wǎng)——每日最新資訊28at.com

K2f28資訊網(wǎng)——每日最新資訊28at.com

4、聚合K2f28資訊網(wǎng)——每日最新資訊28at.com

計(jì)數(shù)、總和、均值等數(shù)據(jù)聚合,是數(shù)據(jù)分析最常執(zhí)行的任務(wù)之一。K2f28資訊網(wǎng)——每日最新資訊28at.com

我們可以使用聚合找到各國的 NewConfimed 病例總數(shù)。使用 groupby 和 agg 函數(shù)執(zhí)行聚合。K2f28資訊網(wǎng)——每日最新資訊28at.com

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

使用 Join 操作將 2 個(gè)數(shù)據(jù)集組合成一個(gè)數(shù)據(jù)集。K2f28資訊網(wǎng)——每日最新資訊28at.com

例如:一個(gè)數(shù)據(jù)集可能包含不同國家/地區(qū)的 Covid-19 病例數(shù),另一個(gè)數(shù)據(jù)集可能包含不同國家/地區(qū)的緯度和經(jīng)度信息。K2f28資訊網(wǎng)——每日最新資訊28at.com

現(xiàn)在我們需要結(jié)合這兩個(gè)信息,那么我們可以執(zhí)行如下所示的連接操作K2f28資訊網(wǎng)——每日最新資訊28at.com

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

了解數(shù)學(xué)內(nèi)建函數(shù),如 min()、max()、mean()、sum() 等,對(duì)于執(zhí)行不同的分析非常有幫助。K2f28資訊網(wǎng)——每日最新資訊28at.com

我們可以通過調(diào)用它們直接在數(shù)據(jù)幀上應(yīng)用這些函數(shù),這些函數(shù)可以在列上或在聚合函數(shù)中獨(dú)立使用,如下所示:K2f28資訊網(wǎng)——每日最新資訊28at.com

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

我們自己編寫的函數(shù)是用戶自定義函數(shù)。我們可以在需要時(shí)通過調(diào)用該函數(shù)來執(zhí)行這些函數(shù)中的代碼。例如,我們可以創(chuàng)建一個(gè)函數(shù)來添加 2 個(gè)數(shù)字,如下所示:K2f28資訊網(wǎng)——每日最新資訊28at.com

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

Pivot 是將一列行內(nèi)的唯一值轉(zhuǎn)換為多個(gè)新列,這是很棒的數(shù)據(jù)處理技術(shù)。K2f28資訊網(wǎng)——每日最新資訊28at.com

在 Covid-19 數(shù)據(jù)集上使用 pivot_table() 函數(shù),我們可以將國家名稱轉(zhuǎn)換為單獨(dú)的新列:K2f28資訊網(wǎng)——每日最新資訊28at.com

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

很多時(shí)候需要遍歷數(shù)據(jù)框的索引和行,我們可以使用 iterrows 函數(shù)遍歷數(shù)據(jù)框:K2f28資訊網(wǎng)——每日最新資訊28at.com

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

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

例如如何將字符串轉(zhuǎn)換為大寫、小寫以及如何找到字符串的長度。K2f28資訊網(wǎng)——每日最新資訊28at.com

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

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

聲明:本網(wǎng)頁內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。郵件:2376512515@qq.com

上一篇: 云安全日?qǐng)?bào)220308:Ubuntu Redis數(shù)據(jù)庫發(fā)現(xiàn)執(zhí)行任意代碼漏洞,需要盡快升級(jí)

下一篇: 騙人還是文字強(qiáng)!MIT最新研究:DeepFake換臉還不如編輯動(dòng)動(dòng)筆

標(biāo)簽:
  • 熱門焦點(diǎn)
Top 主站蜘蛛池模板: 荥经县| 县级市| 揭阳市| 都安| 封丘县| 万源市| 承德县| 富顺县| 保山市| 繁峙县| 乃东县| 西华县| 韶关市| 宁阳县| 岳阳县| 万州区| 衡阳县| 荆州市| 林芝县| 寻甸| 弥渡县| 临桂县| 武川县| 武宣县| 黔南| 扶余县| 河津市| 监利县| 鹤峰县| 班玛县| 太原市| 游戏| 大荔县| 固阳县| 佛冈县| 应城市| 郁南县| 织金县| 牡丹江市| 栾城县| 丹棱县|