当前位置:首页 > 科技  > 知识百科

十个 Python 小技巧,覆盖了90%的数据分析需求!

来源: 责编: 时间:2023-08-07 16:30:00 127观看
导读 数据分析师日常工作会涉及各种任务,比如数据预处理、数据分析、机器学习模型创建、模型部署。在本文中,我将分享10个 Python 操作,它们可覆盖90%的数据分析问题。有所收获点赞

数据分析师日常工作会涉及各种任务,比如数据预处理、数据分析、机器学习模型创建、模型部署。7G428资讯网——每日最新资讯28at.com

在本文中,我将分享10个 Python 操作,它们可覆盖90%的数据分析问题。有所收获点赞、收藏、关注。7G428资讯网——每日最新资讯28at.com

1、阅读数据集7G428资讯网——每日最新资讯28at.com

阅读数据是数据分析的组成部分,了解如何从不同的文件格式读取数据是数据分析师的第一步。下面是如何使用 pandas 读取包含 Covid-19 数据的 csv 文件的示例。7G428资讯网——每日最新资讯28at.com

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

以下是 countries_df.head() 的输出,我们可以使用它查看数据框的前 5 行:7G428资讯网——每日最新资讯28at.com

7G428资讯网——每日最新资讯28at.com

2、汇总统计7G428资讯网——每日最新资讯28at.com

下一步就是通过查看数据汇总来了解数据,例如 NewConfirmed、TotalConfirmed 等数字列的计数、均值、标准偏差、分位数以及国家代码等分类列的频率、最高出现值7G428资讯网——每日最新资讯28at.com

countries_df.describe()7G428资讯网——每日最新资讯28at.com

使用 describe 函数,我们可以得到数据集连续变量的摘要,如下所示:7G428资讯网——每日最新资讯28at.com

7G428资讯网——每日最新资讯28at.com

在 describe() 函数中,我们可以设置参数"include = 'all'"来获取连续变量和分类变量的摘要7G428资讯网——每日最新资讯28at.com

countries_df.describe(include = 'all')7G428资讯网——每日最新资讯28at.com

7G428资讯网——每日最新资讯28at.com

3、数据选择和过滤7G428资讯网——每日最新资讯28at.com

分析其实不需要数据集的所有行和列,只需要选择感兴趣的列并根据问题过滤一些行。7G428资讯网——每日最新资讯28at.com

例如,我们可以使用以下代码选择 Country 和 NewConfirmed 列:7G428资讯网——每日最新资讯28at.com

countries_df[['Country','NewConfirmed']]7G428资讯网——每日最新资讯28at.com

我们还可以将数据过滤Country,使用 loc,我们可以根据一些值过滤列,如下所示:7G428资讯网——每日最新资讯28at.com

countries_df.loc[countries_df['Country'] == 'United States of America']7G428资讯网——每日最新资讯28at.com

7G428资讯网——每日最新资讯28at.com

4、聚合7G428资讯网——每日最新资讯28at.com

计数、总和、均值等数据聚合,是数据分析最常执行的任务之一。7G428资讯网——每日最新资讯28at.com

我们可以使用聚合找到各国的 NewConfimed 病例总数。使用 groupby 和 agg 函数执行聚合。7G428资讯网——每日最新资讯28at.com

countries_df.groupby(['Country']).agg({'NewConfirmed':'sum'})5、Join7G428资讯网——每日最新资讯28at.com

使用 Join 操作将 2 个数据集组合成一个数据集。7G428资讯网——每日最新资讯28at.com

例如:一个数据集可能包含不同国家/地区的 Covid-19 病例数,另一个数据集可能包含不同国家/地区的纬度和经度信息。7G428资讯网——每日最新资讯28at.com

现在我们需要结合这两个信息,那么我们可以执行如下所示的连接操作7G428资讯网——每日最新资讯28at.com

countries_lat_lon = pd.read_excel('C:/Users/anmol/Desktop/Courses/Python for Data Science/Code/countries_lat_lon.xlsx')7G428资讯网——每日最新资讯28at.com
7G428资讯网——每日最新资讯28at.com
# joining the 2 dataframe : countries_df and countries_lat_lon7G428资讯网——每日最新资讯28at.com
# syntax : pd.merge(left_df, right_df, on = 'on_column', how = 'type_of_join')7G428资讯网——每日最新资讯28at.com
joined_df = pd.merge(countries_df, countries_lat_lon, on = 'CountryCode', how = 'inner')7G428资讯网——每日最新资讯28at.com
joined_df6、内建函数7G428资讯网——每日最新资讯28at.com

了解数学内建函数,如 min()、max()、mean()、sum() 等,对于执行不同的分析非常有帮助。7G428资讯网——每日最新资讯28at.com

我们可以通过调用它们直接在数据帧上应用这些函数,这些函数可以在列上或在聚合函数中独立使用,如下所示:7G428资讯网——每日最新资讯28at.com

# finding sum of NewConfirmed cases of all the countries 7G428资讯网——每日最新资讯28at.com
countries_df['NewConfirmed'].sum()7G428资讯网——每日最新资讯28at.com
# Output : 6,631,8997G428资讯网——每日最新资讯28at.com
7G428资讯网——每日最新资讯28at.com
# finding the sum of NewConfirmed cases across different countries 7G428资讯网——每日最新资讯28at.com
countries_df.groupby(['Country']).agg({'NewConfirmed':'sum'})7G428资讯网——每日最新资讯28at.com
7G428资讯网——每日最新资讯28at.com
# Output 7G428资讯网——每日最新资讯28at.com
# NewConfirmed7G428资讯网——每日最新资讯28at.com
#Country 7G428资讯网——每日最新资讯28at.com
#Afghanistan 757G428资讯网——每日最新资讯28at.com
#Albania 1687G428资讯网——每日最新资讯28at.com
#Algeria 2477G428资讯网——每日最新资讯28at.com
#Andorra 07G428资讯网——每日最新资讯28at.com
#Angola 537、用户自定义函数7G428资讯网——每日最新资讯28at.com

我们自己编写的函数是用户自定义函数。我们可以在需要时通过调用该函数来执行这些函数中的代码。例如,我们可以创建一个函数来添加 2 个数字,如下所示:7G428资讯网——每日最新资讯28at.com

# User defined function is created using 'def' keyword, followed by function definition - 'addition()'7G428资讯网——每日最新资讯28at.com
# and 2 arguments num1 and num27G428资讯网——每日最新资讯28at.com
def addition(num1, num2):7G428资讯网——每日最新资讯28at.com
return num1+num27G428资讯网——每日最新资讯28at.com
7G428资讯网——每日最新资讯28at.com
# calling the function using function name and providing the arguments 7G428资讯网——每日最新资讯28at.com
print(addition(1,2))7G428资讯网——每日最新资讯28at.com
#output : 38、Pivot7G428资讯网——每日最新资讯28at.com

Pivot 是将一列行内的唯一值转换为多个新列,这是很棒的数据处理技术。7G428资讯网——每日最新资讯28at.com

在 Covid-19 数据集上使用 pivot_table() 函数,我们可以将国家名称转换为单独的新列:7G428资讯网——每日最新资讯28at.com

# using pivot_table to convert values within the Country column into individual columns and 7G428资讯网——每日最新资讯28at.com
# filling the values corresponding to these columns with numeric variable - NewConfimed 7G428资讯网——每日最新资讯28at.com
pivot_df = pd.pivot_table(countries_df, columns = 'Country', values = 'NewConfirmed')7G428资讯网——每日最新资讯28at.com
pivot_df9、遍历数据框7G428资讯网——每日最新资讯28at.com

很多时候需要遍历数据框的索引和行,我们可以使用 iterrows 函数遍历数据框:7G428资讯网——每日最新资讯28at.com

# iterating over the index and row of a dataframe using iterrows() function 7G428资讯网——每日最新资讯28at.com
for index, row in countries_df.iterrows():7G428资讯网——每日最新资讯28at.com
print('Index is ' + str(index))7G428资讯网——每日最新资讯28at.com
print('Country is '+ str(row['Country']))7G428资讯网——每日最新资讯28at.com
7G428资讯网——每日最新资讯28at.com
# Output : 7G428资讯网——每日最新资讯28at.com
# Index is 07G428资讯网——每日最新资讯28at.com
# Country is Afghanistan7G428资讯网——每日最新资讯28at.com
# Index is 17G428资讯网——每日最新资讯28at.com
# Country is Albania7G428资讯网——每日最新资讯28at.com
# .......10、字符串操作7G428资讯网——每日最新资讯28at.com

很多时候我们处理数据集中的字符串列,在这种情况下,了解一些基本的字符串操作很重要。7G428资讯网——每日最新资讯28at.com

例如如何将字符串转换为大写、小写以及如何找到字符串的长度。7G428资讯网——每日最新资讯28at.com

# country column to upper case7G428资讯网——每日最新资讯28at.com
countries_df['Country_upper'] = countries_df['Country'].str.upper()7G428资讯网——每日最新资讯28at.com
7G428资讯网——每日最新资讯28at.com
# country column to lower case7G428资讯网——每日最新资讯28at.com
countries_df['CountryCode_lower']=countries_df['CountryCode'].str.lower()7G428资讯网——每日最新资讯28at.com
7G428资讯网——每日最新资讯28at.com
# finding length of characters in the country column 7G428资讯网——每日最新资讯28at.com
countries_df['len'] = countries_df['Country'].str.len()7G428资讯网——每日最新资讯28at.com
7G428资讯网——每日最新资讯28at.com
countries_df.head()7G428资讯网——每日最新资讯28at.com

本文链接://www.dmpip.com//www.dmpip.com/showinfo-119-2208-0.html十个 Python 小技巧,覆盖了90%的数据分析需求!

声明:本网页内容旨在传播知识,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。邮件:2376512515@qq.com

上一篇: 云安全日报220308:Ubuntu Redis数据库发现执行任意代码漏洞,需要尽快升级

下一篇: 骗人还是文字强!MIT最新研究:DeepFake换脸还不如编辑动动笔

标签:
  • 热门焦点
Top
Baidu
map