个性化阅读
专注于IT技术分析

使用Python进行数据分析和可视化

点击下载

Python是一种用于数据分析的伟大语言,主要是因为以数据为中心的Python包形成了一个奇妙的生态系统。Pandas就是这些包中的一个,它使导入和分析数据变得更加容易。在这篇文章中,我使用了Pandas来分析Country data .csv文件中的数据,该文件来自一个流行的“statweb.stanford.edu”网站的联合国公共数据集。

由于我已经分析了印度国家的数据,我将Pandas的关键概念介绍如下。在阅读本文之前,先大致了解一下matplotlib和csv的基础知识。

安装

安装Pandas的最简单方法是使用pip:

pip install pandas

或者, 从这里下载:https://pypi.python.org/pypi/pandas/#downloads

在Pandas中创建一个DataFrame

dataframe的创建是通过使用pd将多个系列传递到dataframe类来完成的。系列方法。这里,它传入两个系列对象,s1作为第一行,s2作为第二行。

例子:

# assigning two series to s1 and s2
s1 = pd.Series([ 1 , 2 ])
s2 = pd.Series([ "Ashish" , "Sid" ])
# framing series objects into data
df = pd.DataFrame([s1, s2])
# show the data frame
df
  
# data framing in another way
# taking index and column values
dframe = pd.DataFrame([[ 1 , 2 ], [ "Ashish" , "Sid" ]], index = [ "r1" , "r2" ], columns = [ "c1" , "c2" ])
dframe
  
# framing in another way 
# dict-like container
dframe = pd.DataFrame({
         "c1" : [ 1 , "Ashish" ], "c2" : [ 2 , "Sid" ]})
dframe

输出如下:

用Pandas导入数据

第一步是读取数据。数据存储为逗号分隔的值或csv文件,其中每一行由新行分隔,每列由逗号(,)分隔。为了能够在Python中处理数据,需要将csv文件读入Pandas数据帧。数据帧是一种表示和处理表格数据的方法。表格数据有行和列,就像这个csv文件一样(单击下载:http://data.un.org/countryData/Data/ShowDetail/IND)。

例子:

# Import the pandas library, renamed as pd
import pandas as pd
  
# Read IND_data.csv into a DataFrame, assigned to df
df = pd.read_csv( "IND_data.csv" )
  
# Prints the first 5 rows of a DataFrame as default
df.head()
  
# Prints no. of rows and columns of a DataFrame
df.shape

输出如下:

29, 10

可以使用pandas.DataFrame.iloc方法建立索引。iloc方法允许根据位置检索尽可能多的行和列。

例子:

# prints first 5 rows and every column which replicates df.head()
df.iloc[ 0 : 5 , :]
# prints entire rows and columns
df.iloc[:, :]
# prints from 5th rows and first 5 columns
df.iloc[ 5 :, : 5 ]

可以使用panda.dataframe.loc方法对标签进行索引,该方法允许使用标签而不是位置进行索引。

例子:

# prints first five rows including 5th index and every columns of df
df.loc[ 0 : 5 , :]
# prints from 5th rows onwards and entire columns
df = df.loc[ 5 :, :]

上面的内容实际上与df.iloc [0:5 , :]并没有太大不同。这是因为尽管行标签可以采用任何值, 但我们的行标签与位置完全匹配。但是, 列标签可以使处理数据时变得更加容易。例:

# Prints the first 5 rows of Time period
# value 
df.loc[: 5 , "Time period" ]

DataFrame Math与Pandas

数据帧的计算可以通过使用pandas工具的统计功能来完成。

例子:

# computes various summary statistics, excluding NaN values
df.describe()
# for computing correlations
df.corr()
# computes numerical data ranks
df.rank()

Pandas图

这些示例中的图是使用用于引用matplotlib API的标准约定制作的, 该API提供了Pandas的基础知识, 可轻松创建美观的图。

例子:

# import the required module 
import matplotlib.pyplot as plt
# plot a histogram 
df[ 'Observation Value' ].hist(bins = 10 )
  
# shows presence of a lot of outliers/extreme values
df.boxplot(column = 'Observation Value' , by = 'Time period' )
  
# plotting points as a scatter plot
x = df[ "Observation Value" ]
y = df[ "Time period" ]
plt.scatter(x, y, label = "stars" , color = "m" , marker = "*" , s = 30 )
# x-axis label
plt.xlabel( 'Observation Value' )
# frequency label
plt.ylabel( 'Time period' )
# function to show the plot
plt.show()

使用Python进行数据分析和可视化|套装2

参考:

  • http://pandas.pydata.org/pandas-docs/stable/tutorials.html
  • https://www.srcmini02.com

如果发现任何不正确的地方, 或者想分享有关上述主题的更多信息, 请写评论。

首先, 你的面试准备可通过以下方式增强你的数据结构概念:Python DS课程。

赞(0)
未经允许不得转载:srcmini » 使用Python进行数据分析和可视化

评论 抢沙发

评论前必须登录!