成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

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

大數(shù)據(jù) 數(shù)據(jù)分析 前端
在本文中,我將分享10個(gè) Python 操作,它們可覆蓋90%的數(shù)據(jù)分析問題。有所收獲點(diǎn)贊、收藏、關(guān)注。

數(shù)據(jù)分析師日常工作會(huì)涉及各種任務(wù),比如數(shù)據(jù)預(yù)處理、數(shù)據(jù)分析、機(jī)器學(xué)習(xí)模型創(chuàng)建、模型部署。

在本文中,我將分享10個(gè) Python 操作,它們可覆蓋90%的數(shù)據(jù)分析問題。有所收獲點(diǎn)贊、收藏、關(guān)注。

1、閱讀數(shù)據(jù)集

閱讀數(shù)據(jù)是數(shù)據(jù)分析的組成部分,了解如何從不同的文件格式讀取數(shù)據(jù)是數(shù)據(jù)分析師的第一步。下面是如何使用 pandas 讀取包含 Covid-19 數(shù)據(jù)的 csv 文件的示例。

import pandas as pd 
# reading the countries_data file along with the location within read_csv function.
countries_df = pd.read_csv('C:/Users/anmol/Desktop/Courses/Python for Data Science/Code/countries_data.csv')
# showing the first 5 rows of the dataframe
countries_df.head()

以下是 countries_df.head() 的輸出,我們可以使用它查看數(shù)據(jù)框的前 5 行:

2、匯總統(tǒng)計(jì)

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

countries_df.describe()

使用 describe 函數(shù),我們可以得到數(shù)據(jù)集連續(xù)變量的摘要,如下所示:

在 describe() 函數(shù)中,我們可以設(shè)置參數(shù)"include = 'all'"來獲取連續(xù)變量和分類變量的摘要

countries_df.describe(include = 'all')

3、數(shù)據(jù)選擇和過濾

分析其實(shí)不需要數(shù)據(jù)集的所有行和列,只需要選擇感興趣的列并根據(jù)問題過濾一些行。

例如,我們可以使用以下代碼選擇 Country 和 NewConfirmed 列:

countries_df[['Country','NewConfirmed']]

我們還可以將數(shù)據(jù)過濾Country,使用 loc,我們可以根據(jù)一些值過濾列,如下所示:

countries_df.loc[countries_df['Country'] == 'United States of America']

4、聚合

計(jì)數(shù)、總和、均值等數(shù)據(jù)聚合,是數(shù)據(jù)分析最常執(zhí)行的任務(wù)之一。

我們可以使用聚合找到各國的 NewConfimed 病例總數(shù)。使用 groupby 和 agg 函數(shù)執(zhí)行聚合。

countries_df.groupby(['Country']).agg({'NewConfirmed':'sum'})

5、Join

使用 Join 操作將 2 個(gè)數(shù)據(jù)集組合成一個(gè)數(shù)據(jù)集。

例如:一個(gè)數(shù)據(jù)集可能包含不同國家/地區(qū)的 Covid-19 病例數(shù),另一個(gè)數(shù)據(jù)集可能包含不同國家/地區(qū)的緯度和經(jīng)度信息。

現(xiàn)在我們需要結(jié)合這兩個(gè)信息,那么我們可以執(zhí)行如下所示的連接操作

countries_lat_lon = pd.read_excel('C:/Users/anmol/Desktop/Courses/Python for Data Science/Code/countries_lat_lon.xlsx')

# 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_df

6、內(nèi)建函數(shù)

了解數(shù)學(xué)內(nèi)建函數(shù),如 min()、max()、mean()、sum() 等,對(duì)于執(zhí)行不同的分析非常有幫助。

我們可以通過調(diào)用它們直接在數(shù)據(jù)幀上應(yīng)用這些函數(shù),這些函數(shù)可以在列上或在聚合函數(shù)中獨(dú)立使用,如下所示:

# finding sum of NewConfirmed cases of all the countries 
countries_df['NewConfirmed'].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 53

7、用戶自定義函數(shù)

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

# User defined function is created using 'def' keyword, followed by function definition - 'addition()'
# and 2 arguments num1 and num2
def addition(num1, num2):
return num1+num2

# calling the function using function name and providing the arguments
print(addition(1,2))
#output : 3

8、Pivot

Pivot 是將一列行內(nèi)的唯一值轉(zhuǎn)換為多個(gè)新列,這是很棒的數(shù)據(jù)處理技術(shù)。

在 Covid-19 數(shù)據(jù)集上使用 pivot_table() 函數(shù),我們可以將國家名稱轉(zhuǎn)換為單獨(dú)的新列:

# 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_table(countries_df, columns = 'Country', values = 'NewConfirmed')
pivot_df

9、遍歷數(shù)據(jù)框

很多時(shí)候需要遍歷數(shù)據(jù)框的索引和行,我們可以使用 iterrows 函數(shù)遍歷數(shù)據(jù)框:

# 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、字符串操作

很多時(shí)候我們處理數(shù)據(jù)集中的字符串列,在這種情況下,了解一些基本的字符串操作很重要。

例如如何將字符串轉(zhuǎn)換為大寫、小寫以及如何找到字符串的長度。

# country column to upper case
countries_df['Country_upper'] = countries_df['Country'].str.upper()

# country column to lower case
countries_df['CountryCode_lower']=countries_df['CountryCode'].str.lower()

# finding length of characters in the country column
countries_df['len'] = countries_df['Country'].str.len()

countries_df.head()
責(zé)任編輯:華軒 來源: Python學(xué)習(xí)與數(shù)據(jù)挖掘
相關(guān)推薦

2023-10-04 00:17:00

SQL數(shù)據(jù)庫

2024-01-30 00:40:10

2024-01-30 00:36:41

Python機(jī)器學(xué)習(xí)

2024-02-20 14:25:39

Python數(shù)據(jù)分析

2024-10-15 10:40:09

2013-09-29 13:36:07

虛擬SAN

2010-12-06 09:49:28

Linux快速啟動(dòng)

2024-12-03 14:33:42

Python遞歸編程

2022-10-19 15:20:58

pandas數(shù)據(jù)處理庫技巧

2011-06-01 09:59:52

2021-05-12 09:00:00

WebReactJavaScript

2020-08-21 08:52:09

Python數(shù)據(jù)分析工具

2022-05-12 08:12:51

PythonPip技巧

2009-03-03 16:50:52

需求分析軟件需求需求管理

2023-11-08 18:05:06

Python類型技巧

2024-01-03 08:53:35

JavaScrip編程語言NodeJS

2022-05-06 13:19:13

JS前端

2024-01-06 18:02:18

編程記錄日志

2019-07-25 14:23:36

2019-08-16 02:00:46

AndroidGoogle 移動(dòng)系統(tǒng)
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)

主站蜘蛛池模板: 国产在线不卡视频 | 91视频久久 | 久久久久久国产精品免费免费狐狸 | 欧美在线观看免费观看视频 | 日日摸日日碰夜夜爽亚洲精品蜜乳 | 日韩精品久久一区二区三区 | 亚洲国产精品久久人人爱 | 视频三区 | 一级片av | 国产 日韩 欧美 在线 | 在线播放中文字幕 | 九九色综合 | 午夜丁香视频在线观看 | 自拍偷拍精品 | 99热都是精品| 国产精品欧美日韩 | 国产免费一区二区 | 伊人二区| 草久久 | 久热精品视频 | 精品一区二区av | 久久国产精品首页 | 欧美一区二区在线免费观看 | 国产精品亚洲精品日韩已方 | 国产草草视频 | 成人久久视频 | 一区二区影院 | 最新国产在线 | 日本网站免费在线观看 | 成人午夜电影在线观看 | 国产日韩欧美二区 | 日本 欧美 国产 | 欧美亚洲综合久久 | 日本又色又爽又黄又高潮 | 99亚洲精品 | av看片网站| 99色综合 | 伊人中文字幕 | 成人av片在线观看 | 色婷婷亚洲一区二区三区 | 精品一区二区久久久久久久网站 |