在许多情况下,我们将数据分成多个集合,并在每个子集上应用一些函数。在应用函数中,可以执行以下操作。
将数据进行分组
import pandas as pd import numpy as np df = pd.DataFrame(np.arange(18).reshape(6,3), index = list('abcdef'), columns = ['one','two','three']) print(df.groupby('one')) <pandas.core.groupby.groupby.DataFrameGroupBy object at 0x10387dd68>使用get_group()方法,选择一个组
import pandas as pd import numpy as np df = pd.DataFrame(np.arange(18).reshape(6,3), index = list('abcdef'), columns = ['one','two','three']) groupd = df.groupby('one') print(groupd.get_group(3)) one two three b 3 4 5聚合函数为每个组返回单个聚合值。 当创建了分组(group_by)对象,就可以对分组数据执行多个聚合操作。 常用的是通过聚合或等效的agg方法聚合。
import pandas as pd import numpy as np df = pd.DataFrame(np.arange(18).reshape(6,3), index = list('abcdef'), columns = ['one','two','three']) print(df) grouped = df.groupby('one') print(grouped['three'].agg(np.mean)) print(grouped['three'].agg(np.size)) # 查看分组大小 one two three a 0 1 2 b 3 4 5 c 6 7 8 d 9 10 11 e 12 13 14 f 15 16 17 one 0 2 3 5 6 8 9 11 12 14 15 17 Name: three, dtype: int64 one 0 1 3 1 6 1 9 1 12 1 15 1 Name: three, dtype: int64传递函数的列表或字典来进行聚合
import pandas as pd import numpy as np df = pd.DataFrame(np.arange(18).reshape(6,3), index = list('abcdef'), columns = ['one','two','three']) print(df) grouped = df.groupby('one') print(grouped['three'].agg([np.sum, np.mean, np.std])) sum mean std one 0 2 2 NaN 3 5 5 NaN 6 8 8 NaN 9 11 11 NaN 12 14 14 NaN 15 17 17 NaNPandas提供了一个单独的merge()函数,作为DataFrame对象之间所有标准数据库连接操作的入口
pd.merge(left, right, how='inner', on=None, left_on=None, right_on=None, left_index=False, right_index=False, sort=True) 参数描述left另一个DataFrame对象right另一个DataFrame对象on列(名称)连接,必须在左和右DataFrame对象中存在。letf_on左侧DataFrame中的列用作键,可以是列名或长度等于DataFrame长度的数组。right_on右侧的DataFrame的列作为键,可以是列名或长度等于DataFrame长度的数组。left_index如果为True,使用左侧DataFrame中的索引(行标签)作为其连接键fight_index同上how默认inner。可选left、right、outher、innersort按照字典顺序通过连接键对结果DataFrame进行排序。默认为True,设置为False时,在很多情况下大大提高性能。如何合并参数指定如何确定哪些键将被包含在结果表中。如果组合键没有出现在左侧或右侧表中,则连接表中的值将为NA。
left:使用左侧对象的键right:使用右侧对象的键outher:使用键的联合inner:使用键的交集 import pandas as pd import numpy as np left = pd.DataFrame({ 'id':[1,2,3,4], 'Name':['hubo','vim','vi','kaka'], 'answer_id':['sub1','sub2','sub3','sub4'] }) right = pd.DataFrame({ 'id':[1,2,3,4], 'Name':['li','bo','wn','su'], 'answer_id':['sub2','sub5','sub3','sub4'] }) mid = pd.merge(left,right,on=['answer_id'], how='left') print(mid) id_x Name_x answer_id id_y Name_y 0 1 hubo sub1 NaN NaN 1 2 vim sub2 1.0 li 2 3 vi sub3 3.0 wn 3 4 kaka sub4 4.0 su读取文件主要功能是read_csv()和read_table() 使用相同的解析来智能的将表格数据转换为DataFrame对象
import pandas as pd df = pd.read_excel('catering_sale.xls') print(df.head()) 日期 销量 0 2015-03-01 51.0 1 2015-02-28 2618.2 2 2015-02-27 2608.4 3 2015-02-26 2651.9 4 2015-02-25 3442.1指定文件的一列来使用index_col定制索引
import pandas as pd df = pd.read_excel('catering_sale.xls', index_col='日期') print(df.head()) 销量 日期 2015-03-01 51.0 2015-02-28 2618.2 2015-02-27 2608.4 2015-02-26 2651.9 2015-02-25 3442.1dtype的列可以作为字典传递 将int类型变为float类型
import pandas as pd import numpy as np # df = pd.read_excel('catering_sale.xls', dtype={'销售':np.float64}) df = pd.read_excel('catering_sale.xls', dtype={'销售':np.float64}) print(df.dtypes) 日期 datetime64[ns] 销量 float64 dtype: object替换标题,并且从第二行开始显示
import pandas as pd import numpy as np df = pd.read_excel('catering_sale.xls', names=['a','b'], header=1) print(df.head()) a b 0 2015-02-28 2618.2 1 2015-02-27 2608.4 2 2015-02-26 2651.9 3 2015-02-25 3442.1 4 2015-02-24 3393.1