• 一、Python读取文件的方法
  • 二、数据的基础描述
  • 三、DataFrame 排序、增删行列操作
  • 四、DataFrame数据处理:求和、平均数、计数
  • 五、DataFrame的增加行列合并操作等


以下为具体内容 一、Python读取文件的方法 Python读取excel文件有以下三种方式,个人推荐Pandas读取法 1、 利用pandas读取 个人比较喜欢直接使用pandas 保存和读取数据,方便好用,特别推荐,只需要三行代码就可以搞定

import pandas as pddata = pd.read_excel('/Users/***/Fortune500.xlsx')data
import pandas as pd

data = pd.read_excel('/Users/***/Fortune500.xlsx')
data




python怎么使用describe对一组数据 describe python_python (describe())


2、 使用xlrd读取Excel 先安装:pip install xlrd,然后读取

import xlrd    # 打开文件 data = xlrd.open_workbook('filename.xlsx')data.sheet_names()  # 获取所有sheet名字 data.sheetsdata.sheets()       # 获取所有sheet对象sheet1 = data.sheet_by_name("test")  # 通过sheet名查找sheet2 = data.sheet_by_index(3)  # 通过索引查找rows = sheet1.row_values(2)#获取行内容cols = sheet1.col_values(3)#获取列内容
import xlrd    # 打开文件 data = xlrd.open_workbook('filename.xlsx')
data.sheet_names()  # 获取所有sheet名字 data.sheets

data.sheets()       # 获取所有sheet对象
sheet1 = data.sheet_by_name("test")  # 通过sheet名查找
sheet2 = data.sheet_by_index(3)  # 通过索引查找
rows = sheet1.row_values(2)#获取行内容
cols = sheet1.col_values(3)#获取列内容



3、使用openpyxl库读取Excel 先直接用pip命令安装 openpyxl:pip3 install openpyxl

from openpyxl import load_workbookexcel=load_workbook('/test.xlsx')table = excel.get_sheet_by_name('Sheet1')rows=table.max_row #获取行数cols=table.max_column #获取列数
from openpyxl import load_workbook

excel=load_workbook('/test.xlsx')
table = excel.get_sheet_by_name('Sheet1')

rows=table.max_row #获取行数

cols=table.max_column #获取列数



二、数据的基础描述 一般情况下,会使用 describe、dtypes、info、head等函数简单查看数据的情况和质量,是否需要进一步处理




df.describe()    #汇总统计df.dtypes   #数据类型data.shapedata.info()data.head()  #取前几行data.columns   #观察列名称data.index  #观察行名称
df.describe()    #汇总统计
df.dtypes   #数据类型
data.shape
data.info()
data.head()  #取前几行
data.columns   #观察列名称
data.index  #观察行名称







python怎么使用describe对一组数据 describe python_groupby python_02


三、DataFrame 排序、增删行列操作 排序: sort_values (["Revenue","Profit"],ascending=False),根据列来降序或者升序排列

import pandas as pddata=pd.read_excel('/Users/samuelzhan/爬虫/Fortune500.xlsx')data.describe()data.shapedata.sort_values(["Revenue","Profit"],ascending=False)
import pandas as pd

data=pd.read_excel('/Users/samuelzhan/爬虫/Fortune500.xlsx')
data.describe()
data.shape

data.sort_values(["Revenue","Profit"],ascending=False)




python怎么使用describe对一组数据 describe python_.describe() python_03


删除列: drop('列名',axis=1,inplace=True) 对比上图即可观察除多余的一列数据被删除

python怎么使用describe对一组数据 describe python_python (describe())_04


四、DataFrame数据处理:求和、平均数、计数 求和、计数:都是先选择groupby,然后再汇总分析,案例中是根据国家来做汇总分析,你可以根据行业来汇总分析 此外,在做计数统计中为了减少其他多余的数据,只取了 Country和Revenue两列数据,使用的是 data.loc[:,['Country','Revenue']],Dataframe中提取行列的方法就不做赘述了。

import pandas as pddata=pd.read_excel('/Users/samuelzhan/爬虫/Fortune500.xlsx')data.describe()data.shapedata.sort_values(["Revenue","Profit"],ascending=False)data.drop(['Unnamed: 0'],axis=1,inplace=True)data.groupby('Country').sum()data.loc[:,['Country','Revenue']].groupby('Country').count().rename(columns = {"Revenue": "Count"})
import pandas as pd

data=pd.read_excel('/Users/samuelzhan/爬虫/Fortune500.xlsx')
data.describe()
data.shape

data.sort_values(["Revenue","Profit"],ascending=False)

data.drop(['Unnamed: 0'],axis=1,inplace=True)
data.groupby('Country').sum()
data.loc[:,['Country','Revenue']].groupby('Country').count().rename(columns = {"Revenue": "Count"})




python怎么使用describe对一组数据 describe python_groupby python_05

python怎么使用describe对一组数据 describe python_python (describe())_06


五、DataFrame的增加行列合并操作 上面我们可以看到,一张图汇总了 不同国家500强企业的收入、利润、员工人数的汇总;一张图汇总了不同国家500强的数量,那如何让他们在一张表中展示出来呢?

import pandas as pddata=pd.read_excel('/Users/samuelzhan/爬虫/Fortune500.xlsx')data.describe()data.shapedata.sort_values(["Revenue","Profit"],ascending=False)data.drop(['Unnamed: 0'],axis=1,inplace=True)s1=data.groupby('Country').sum()s2=data.loc[:,['Country','Revenue']].groupby('Country').count().rename(columns = {"Revenue": "Count"})merge = pd.merge(s1,s2,on=['Country'],how='inner')merge.sort_values(by='Count',axis = 0,ascending = False)
import pandas as pd

data=pd.read_excel('/Users/samuelzhan/爬虫/Fortune500.xlsx')
data.describe()
data.shape

data.sort_values(["Revenue","Profit"],ascending=False)

data.drop(['Unnamed: 0'],axis=1,inplace=True)
s1=data.groupby('Country').sum()
s2=data.loc[:,['Country','Revenue']].groupby('Country').count().rename(columns = {"Revenue": "Count"})


merge = pd.merge(s1,s2,on=['Country'],how='inner')
merge.sort_values(by='Count',axis = 0,ascending = False)




python怎么使用describe对一组数据 describe python_.describe() python_07


这里使用了Merge的方法,一般表链接有三种方式: 1、merge 相当于SQL中的JOIN。该函数的典型应用场景是,两张表有相同内容的列(即SQL中的键),现在我们想把两张表整合到一张表里。在此典型情况下,结果集的行数并没有增加,列数则为两个元数据的列数和减去连接键的数量。 2、concat 轴向连接。

就是单纯地把两个表拼在一起,这个过程也被称作绑定(binding)或堆叠(stacking)。因此可以想见,这个函数的关键参数应该是 axis,用于指定连接的轴向。axis=1 在行中操作,axis=0是在列中操作。默认是axis=0,即垂直堆叠。 3、join

加上join参数的属性,如果为’inner’得到的是两表的交集,如果是outer,得到的是两表的并集。 以上,用爬取的500强数据,简单说明了Python DataFrame数据处理的基本方法,下次我们再来讲解如何进行可视化操作。