size() 和count()的区别
size跟count的区别: size计数时包含NaN值,而count不包含NaN值
user_df = user_df.groupby(['uid','time'])[['qid_time']].count()
# 返回值为DF
user_df = user_df.groupby(['uid','time'])[['qid_time']].size()..reset_index(name='new_columns')
# 返回值为Series,需要.reset_index(name='new_columns')多列才能转成DF
df.drop()和del df[‘column’]的区别
-
直接del DF[‘column-name’] 被普遍认为不是最好的方法,建议慎用,参考:https://stackoverflow.com/questions/13411544/delete-column-from-pandas-dataframe-by-column-name
-
采用drop方法,有下面三种等价的表达式:
DF= DF.drop(‘column_name’, 1);
DF.drop(‘column_name’,axis=1, inplace=True)
DF.drop([DF.columns[[0,1,]]], axis=1,inplace=True)
参考:https://blog.csdn.net/claroja/article/details/65661826
loc 和iloc 的用法
loc[row][‘column_name’]就可以取得对应row行数的字段值
user_df = user_df.groupby(['uid','time'])[['qid_time']].mean().reset_index()
# 索引重建后就是行号
user_df = user_df.groupby(['uid','time'])[['qid_time']].mean()
# 这种方式索引不是顺序自增,无法按行用行号索引遍历
print(type(user_df))
# 多值输出,格式转为Series了(一般用的很少)
print(user_df.loc[2][['time','uid']])
<class 'pandas.core.series.Series'>
time 2019-01-06 12:03:35
uid 128758
# 这样可以唯一定位某一行某一列
print(user_df.loc[2,'time'])
2019-01-06 12:03:35
# 定位 多行 一列
print(user_df.loc[2:5,['time','uid']])
time uid
2 2019-01-06 12:03:35 128758
3 2019-01-06 12:05:35 128758
4 2019-01-06 12:07:35 128758
5 2019-01-06 12:09:35 128758
# 定位 多行 多列
print(user_df.loc[2:5,'uid':'qid_time'])
uid time qid_time
2 128758 2019-01-06 12:03:35 2.0
3 128758 2019-01-06 12:05:35 2.0
4 128758 2019-01-06 12:07:35 2.0
df.loc[1:3, ['total_bill', 'tip']]
df.loc[1:3, 'tip': 'total_bill']
df.iloc[1:3, [1, 2]]
df.iloc[1:3, 1: 3]
df.at[3, 'tip']
df.iat[3, 1]
df.ix[1:3, [1, 2]]
df.ix[1:3, ['total_bill', 'tip']]
df[1: 3]
df[['total_bill', 'tip']]
# df[1:2, ['total_bill', 'tip']] # TypeError: unhashable type
参考:https://www.cnblogs.com/en-heng/p/5630849.html
.reset_index(drop=Ture)的解释
对于经过运算的数据来说,默认索引可能是计算时的基准列,不重置的话索引没有规律,不利于定位,索引可能是单列或者多列的组合索引,重置后统一为一列自增索引,如果选择drop的话,原索引会被删除,不drop的话老的索引会成为新结果的普通列数据(单列或多列),索引默认是不会存入表中(也可以在选择保存索引),索引也不是能参与计算的数据,只是表的一个属性
- 对Series.reset_index()
print(type(user_df['uid']))
print(user_df['uid'].head(3))
print(type(user_df['uid'].reset_index()))
print(user_df['uid'].reset_index().head(3))
print(type(user_df['uid'].reset_index(drop=True)))
print(user_df['uid'].reset_index(drop=True).head(3))
# 输出:
<class 'pandas.core.series.Series'>
0 128758
1 128758
2 128758
<class 'pandas.core.frame.DataFrame'>
index uid
0 0 128758
1 1 128758
2 2 128758
<class 'pandas.core.series.Series'>
0 128758
1 128758
2 128758
# 其他例子:
user_df = user_df.groupby(['uid'])['qid_time'].mean()
<class 'pandas.core.series.Series'>
uid
128758 0.918033
181094 0.086957
182392 0.655738
user_df = user_df.groupby(['uid'])['qid_time'].mean().reset_index()
# 此处因为多了新的一列,所以Series的类型自动转化成DataFrame类型,
# 若drop=true,那数据还是一列的情况下,不会转化成Dataframe
uid qid_time
0 128758 0.918033
1 181094 0.086957
2 182392 0.655738
- 对DF.reset_index()
print(type(user_df[['uid']]))
print(user_df[['uid']].head(3))
print(type(user_df[['uid']].reset_index()))
print(user_df[['uid']].reset_index().head(3))
print(type(user_df[['uid']].reset_index(drop=True)))
print(user_df[['uid']].reset_index(drop=True).head(3))
输出:
<class 'pandas.core.frame.DataFrame'>
uid
0 128758
1 128758
2 128758
<class 'pandas.core.frame.DataFrame'>
index uid # index列表示原来的索引列
0 0 128758
1 1 128758
2 2 128758
<class 'pandas.core.frame.DataFrame'>
uid
0 128758
1 128758
2 128758
# 其他例子
# 未重置之前
user_df = user_df.groupby(['uid'])[['qid_time']].mean()
qid_time
uid
128758 0.918033
181094 0.086957
182392 0.655738
# 重置索引后,将原来的索引列uid转化为普通列,新增自增索引列
user_df = user_df.groupby(['uid'])[['qid_time']].mean().reset_index()
uid qid_time
0 128758 0.918033
1 181094 0.086957
2 182392 0.655738
# 重置索引后,添加drop=Ture,表示将原来的默认索引列uid删除
user_df = user_df.groupby(['uid'])[['qid_time']].mean().reset_index(drop=True)
qid_time
0 0.918033
1 0.086957
2 0.655738
df.groupby().agg() ,df. groupby().apply()
groupdf=df.groupby(df['key1'])
for name,group in groupdf:
print group # 分完组的小组 dataframe类型对象
# print name # name 是分组的关键字
分组统计任务,保留索引列
user_df[['uid','qid','min','max','mean']]=user_df.groupby(['uid','qid']
.agg({'qid_time':[np.min, np.max, np.mean]}).reset_index()
实现sql中的group_concat功能
df.groupby('team').apply(lambda x: ','.join(x.user))
df.groupby('team').apply(lambda x: list(x.user))
df.groupby('team').agg({'user' : lambda x: ', '.join(x)})
参考:https://stackoverflow.com/questions/18138693/replicating-group-concat-for-pandas-dataframe
dataframe需要操作列时,啥时候用单中括号/双中括号?
- 当直接使用df后接一个中括号时,表示取其一列,类型为Series,接2个中括号时,也是取一列,但类型为DataFrame(带有列名)
print(type(df['uid']))
<class 'pandas.core.series.Series'>
# 输出如下
0 128758.0
1 128758.0
2 128758.0
3 128758.0
print(type(user_df[['uid']]))
<class 'pandas.core.frame.DataFrame'>
# 输出如下
uid
0 128758.0
1 128758.0
2 128758.0
3 128758.0
- groupby进行分组聚合时
print(type(user_df['uid']))
print(type(user_df[['uid']]))
print(type(user_df['uid','qid_time']))
print(type(user_df[['uid', 'qid_time']]))
<class 'pandas.core.series.Series'>
<class 'pandas.core.frame.DataFrame'>
错误,没有这种语法
<class 'pandas.core.frame.DataFrame'>
# 单括号,则输出时不会带有列标签,末尾会单独输出一行属性列,输出类型为Series
user_df = user_df.groupby(['uid'])['qid_time'].mean()
print(type(user_df))
<class 'pandas.core.series.Series'>
uid
128758 0.918033
181094 0.086957
182392 0.655738
# 双括号 ,若对需要聚合的单列使用双中括号,则输出时会带有列标,输出类型为DF
user_df = user_df.groupby(['uid'])[['qid_time']].mean()
print(type(user_df))
<class 'pandas.core.frame.DataFrame'>
# 输出如下,但不带排序自增索引列,此处uid为默认索引列
qid_time
uid
128758 0.918033
181094 0.086957
182392 0.655738
# 若需要将多列进行聚合时,单中括号和双中括号没有区别
user_df = user_df.groupby(['uid'])['qid_time','time'].mean()
user_df = user_df.groupby(['uid'])[['qid_time','time']].mean()
# 两个输出一致,因为结果集至少两列,所以类型只能是DataFrame,单括号的结果也会自动转化
# 在groupby()的括号中的参数只能是Series,多列分组使用[column1,column2],不能采用[['column1,columns2]]或df[[column1,columns2]]传参,可以使用df[column1],column只能为一个,因为df['column1']类型也为Series
user_df = user_df.groupby(['uid','time'])[['qid_time']].mean()
<class 'pandas.core.frame.DataFrame'>
qid_time
uid time
128758 2019-01-06 11:55:35 2.000000
2019-01-06 12:01:35 1.000000
2019-01-06 12:03:35 2.000000
user_df = user_df.groupby(['uid','time'])['qid_time'].mean()
<class 'pandas.core.series.Series'> # 这里类型的区别在于后面的单双括号
uid time # 此处两列作为一个组合索引
128758 2019-01-06 11:55:35 2.000000
2019-01-06 12:01:35 1.000000
2019-01-06 12:03:35 2.000000
user_df = user_df.groupby(['uid','time'])['qid_time'].mean().reset_index()
<class 'pandas.core.frame.DataFrame'>
# 此处两列作为一个组合索引,重置后为常规列
uid time qid_time
0 128758 2019-01-06 11:55:35 2.000000
1 128758 2019-01-06 12:01:35 1.000000
2 128758 2019-01-06 12:03:35 2.000000
user_df = user_df.groupby(['uid','time'])['qid_time'].mean().reset_index(drop=True)
<class 'pandas.core.series.Series'> # 两列索引同时删除,生成新索引
0 2.000000
1 1.000000
2 2.000000
series 如何转成DataFrame?
如果是单列索引的Series
s.to_frame(name='column_name')
如果是多列组合索引
s.reset_index(name='column_name')