您现在的位置是:热点 >>正文
十个 Python 小技巧,覆盖了90%的数据分析需求!
热点1751036人已围观
简介...
数据分析师日常工作会涉及各种任务,比如数据预处理、数据分析、机器学习模型创建、模型部署。
在本文中,我将分享10个 Python 操作,它们可覆盖90%的数据分析问题。有所收获点赞、收藏、关注。
1、阅读数据集
阅读数据是数据分析的组成部分,了解如何从不同的文件格式读取数据是数据分析师的第一步。下面是如何使用 pandas 读取包含 Covid-19 数据的 csv 文件的示例。
import pandas as pd
# reading the countries_data file along with t十个he 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() 的输出,我们可以使用它查看数据框的前 5 行:
2、汇总统 P计
下一步就是通过查看数据汇总来了解数据,例如 NewConfirmed、TotalConfirmed 等数字列的计数、均值、标准偏差、分位数以及国家代码等分类列的频率、最高出现值
countries_df.describe()
使用 describe 函数,我们可以得到数据集连续变量的摘要,如下所示:
在 describe() 函数中,我们可以设置参数"include = 'all'"来获取连续变量和分类变量的摘要
countries_df.describe(include = 'all')
3、数据选yt择和过滤
分析其实不需要数据集的所有行和列,只需要选择感兴趣的列并根据问题过滤一些行。
例如,我们可以使用以下代码选择 Country 和 NewConfirmed 列:
countries_df[['Country','NewConfirmed']]
我们还可以将数据过滤Country,使用 loc,我们可以根据一些值过滤列,如下所示:
countries_df.loc[countries_df['Country'] == 'United States of America']
4、聚合
计数、总和ho、均值等数据聚合,是数据分析最常执行的任务之一。
我们可以使用聚合找到各国的 NewConfimed 病例总数。使用 groupby 和 agg 函数执行聚合。
countries_df.groupby(['Country']).agg({'NewConfirmed':'sum'})5、Join
使用 Join 操作将 2 个数据集组合成一个数据集。
例如:一个数据集可能包含不同国家/地区的 Covid-19 病例数,另一个数据集可能包含不同国家/地区的纬度和经度信息。
现在我们需要结合这两个n 信息,那么我们可以执行如下所示的连接操作
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_df6、内建函数
了解数学内建函数,如 min()、max()、mean()、sum() 等,对于执行不同的分析非常有帮助。
我们可以通过调用它们直接在数据帧上应用这些函数,这些函数可以在列上或在聚合函数中独立使用,如下所示:
# finding sum of NewConf巧,irmed 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
#Afghanist覆盖an 75
#Albania 168
#Algeria 247
#Andorra 0
#Angola 537、用户自定义函数
我们自己编写的函数是用户自定义函数。我们可以在需要时通过调用该函数来执行这些函数中的代码。例如,我们可以创建一个函数来添加 2 个数字,如下所示:
# User defined function is created using 'def' keyword, followed by function definition - 'addition()'
# and 2 arguments num1 and num了92
def 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 to0% 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_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 i据分s 1
# Country is Albania
# .......10、字符串操作
很多时候我们处理数据集中的字符串列,在这种情况下,了解一些基本的字符串操作很重要。
例如如何将字符串转换为大写、小写以及如何找到字符串的长度。
# country column to upper case
countries_df['Country_upper'] = countries_df['Country'].str.upper()
# country column to lower case
countries_df['Count析需ryCode_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()
Tags:
相关文章
标签: VITURE Luma Beast XR Cyberpunk 2077
热点云梦级3月3日云梦级消息,XR 眼镜厂商 VITURE 宣布,在 2025 年 9 月完成 1 亿美元(现汇率约合 6.9 亿元人民币) B 轮融资后,如今公司再次获得 1 亿美元新增融资。 据悉,本...
【热点】
阅读更多篮球一级运动员
热点前言:国家一级篮球运动员有什么么条件篮球运动员凡符合下列条件之一,都可以申请一级运动员称号。1.参加全国运动会获决赛权的各队运动员从事三年以上专业训练。上场时间累计不少于50%,申请人数:第一至四名,...
【热点】
阅读更多390亿没了!库克不讲武德,国产巨头惨遭苹果“抛弃”
热点一入豪门深似海,一入苹果产业链就被抓住了小辫子。 苹果公司,作为世界顶级的科技巨头存在,每一代产品仿佛都是一块被自然精雕细琢的和氏璧,也因此几乎所有苹果的产品都受到了全球的追捧,这也让苹果变成了香饽饽...
【热点】
阅读更多
热门文章
最新文章
友情链接
- 标签: 康师傅
- https://www.unclefossil.com/search/谷歌自动批量发帖软件【tg:seogoogle987】gad_source=1&gclid=Cj0KCQjw_-GxBhC1ARIsADGgDjsNKplUXa2F6YGlHBK2sHDQ88cgsJBb05DyO99TV9jNq_cT0kGMqyQaAgHQEALw_wcB
- https://wwwcp.umes.edu/search/谷歌自动批量发帖软件【tg:seogoogle987】,udd
- https://www.resilienz-akademie.com/?cat=71&s=谷歌自动批量发帖软件【tg:seogoogle987】,1HJ
- https://www.kawainter.com/catalogsearch/result/?q=谷歌自动批量发帖软件【tg:seogoogle987】,Qwr
- https://www.niaogebiji.com/pc/article/search?keyword=谷歌自动批量发帖软件【tg:seogoogle987】,f9G
- https://www.parriscues.com/catalogsearch/result/?q=谷歌自动批量发帖软件【tg:seogoogle987】,UDm
- https://www.wellbees.com/index.php/catalogsearch/result/index/?q=谷歌自动批量发帖软件【tg:seogoogle987】,7l5
- https://www.kissthekangaroo.com.au/index.php?route=product/search&tag=谷歌自动批量发帖软件【tg:seogoogle987】,hiR
- https://www.pacific-lifestyle.co.uk/search?sort=alphabetical&term=谷歌自动批量发帖软件【tg:seogoogle987】,Hck
- https://store.line.me/search/sticker/ja?q=谷歌自动批量发帖软件【tg:seogoogle987】J39
- https://www.tiktok.com/tag/谷歌自动批量发帖软件【tg:seogoogle987】,ZJa
- https://www.nbm.org/?action=QUERY&s=谷歌自动批量发帖软件【tg:seogoogle987】,NTL
- https://www.smics.com/tc/site/search?token=谷歌自动批量发帖软件【tg:seogoogle987】,O4y
- https://www.yli.ro/catalogsearch/result/?q=谷歌自动批量发帖软件【tg:seogoogle987】,MIo
- https://www.teamlab.art/th/?submit=谷歌自动批量发帖软件【tg:seogoogle987】,6oj
- https://www.tullygroup.us/?ID=hbkq8jFjrQlVRrwzEmvoUJJAY4e1Lfo5&s=谷歌自动批量发帖软件【tg:seogoogle987】,FPE
- https://www.wmfe.org/?s=谷歌自动批量发帖软件【tg:seogoogle987】,uHu
- https://www.hanbat.ac.kr/search/front/Search.jsp?qt=谷歌自动批量发帖软件【tg:seogoogle987】pxR
- https://www.le-trend.com/en/search/=谷歌自动批量发帖软件【tg:seogoogle987】,MMs
- https://www2.ual.es/ProyectoLates/?s=谷歌自动批量发帖软件【tg:seogoogle987】,PKo
- https://www.vfw.org/search-results?q=谷歌自动批量发帖软件【tg:seogoogle987】,sCJ
- https://www.sebson.de/en/search?query=谷歌自动批量发帖软件【tg:seogoogle987】,kkg
- https://www.remit.co.jp/pt/?action=CHECK&s=谷歌自动批量发帖软件【tg:seogoogle987】,Zgt
- https://dict.naver.com/dict.search?dicQuery&query=谷歌自动批量发帖软件【tg:seogoogle987】jGc
- https://www.kelsa.co.uk/catalogsearch/result/index/?T=1686132119291&q=谷歌自动批量发帖软件【tg:seogoogle987】,TnK
- https://www.sensorshop24.de/catalogsearch/result/index/?type=132&q=谷歌自动批量发帖软件【tg:seogoogle987】,RPe
- https://www.springfieldlibrary.org/library/?s=谷歌自动批量发帖软件【tg:seogoogle987】,g2k
- https://www.monroecounty.gov/search/?q=谷歌自动批量发帖软件【tg:seogoogle987】,ueJ
- https://www.udc.co.uk/index.php?route=product/search&sort=p.price&order=DESC&tag=谷歌自动批量发帖软件【tg:seogoogle987】,o80
- https://www.dba.dk/soeg/?soeg=谷歌自动批量发帖软件【tg:seogoogle987】tav
- https://www.scivee.tv/?s=谷歌自动批量发帖软件【tg:seogoogle987】,qxw
- https://www.sweat.com/collections/vendors?q=谷歌自动批量发帖软件【tg:seogoogle987】,uv8
- https://www.tombow.com/en/search/谷歌自动批量发帖软件【tg:seogoogle987】,u9D
- https://www.voices.com/talents/search?keywords=谷歌自动批量发帖软件【tg:seogoogle987】,4fG
- https://www.welshmountainzoo.org/shop/index.php?routeproduct/search&tag=谷歌自动批量发帖软件【tg:seogoogle987】,frJ
- https://www.spicebar.de/catalogsearch/result/?q=谷歌自动批量发帖软件【tg:seogoogle987】,dae
- https://www.umc.edu/search.aspx?ID=I4gnDKRS991MFcIDDzoYze4aWSAszAm7&q=谷歌自动批量发帖软件【tg:seogoogle987】,byz
- https://www.paspaley.com/catalogsearch/result/?action=QUERY&q=谷歌自动批量发帖软件【tg:seogoogle987】,Xz3
- https://www.pinkpanda.ro/magazin/ingrijirea-tenului?page=87&q=谷歌自动批量发帖软件【tg:seogoogle987】,9z5
- https://www.kuaikanmanhua.com/sou/谷歌自动批量发帖软件【tg:seogoogle987】,v71
- https://www.luxurysociety.com/en/search?q=谷歌自动批量发帖软件【tg:seogoogle987】,Hue
- https://www.wou.edu.my/?s=谷歌自动批量发帖软件【tg:seogoogle987】,LnA
- https://www.londontheatredirect.com/search/谷歌自动批量发帖软件【tg:seogoogle987】,ZkL
- https://www.prattlibrary.org/search?q=谷歌自动批量发帖软件【tg:seogoogle987】,G6D
- https://www.rookwinkel.nl/catalogsearch/result/index/?q=谷歌自动批量发帖软件【tg:seogoogle987】,XVP
- https://www.ncetm.org.uk/search?action=CHECK&q=谷歌自动批量发帖软件【tg:seogoogle987】,BY2
- https://www.vocabulary.com/dictionary/谷歌自动批量发帖软件【tg:seogoogle987】,aw1
- https://www.so.com/s?ie=utf-8&src=360sou_home&_re=0&q=谷歌自动批量发帖软件【tg:seogoogle987】,KQM
- https://www.riohondo.edu/?action=QUERY&s=谷歌自动批量发帖软件【tg:seogoogle987】,JzG
- https://www.goodreads.com/quotes/tag/谷歌自动批量发帖软件【tg:seogoogle987】7wh