#这里讲演pandas的汇总与统计
import numpy as np
import pandas as pd
#我自己的私有函数库,我都加了t_+函数名的
t_data={
"name":["唐浩","小王","老王","赵三","李四","王姐"],
"sex":["男","女","男","女","男","女"],
"year":[37,22,15,18,33,25],
"city":["成都","北京","上海","成都","深圳","北京"]
}
df1=pd.DataFrame(np.arange(0,20).reshape(5,4),index=[1,2,3,4,5],columns=["A","B","C","D"])
print(df1)
#列求和
# df2=df1.sum(axis=1) #x轴进行行求和
# print(df2)
# df22=df1.sum(axis=0) # 默认,y轴列求和
# print(df22)
#describe方法,很有用,对所有数值型列进行统计
#eg :
df222=df1.describe() #只能是列,行无效,无 axis选项
print(df222)
df2222=df222.applymap(lambda x:"%.2f"%x)
print(df2222)
#下面这个是什么意思
print(df2222.index) #Index([‘count个数‘, ‘mean平均值‘, ‘std‘, ‘min‘, ‘25%‘, ‘50%‘, ‘75%‘, ‘max‘]
#所以要拿到A列的个数,平均值,最大值,最小值,std...
# print(df222.ix["min","D"]) #想拿什么就是什么,这个要报警,那就下面这种方法了
print(df222["D"]["max"]) #df222["列名即key"]["索引标签"]这样同样能拿到我想要的
print(df2222["A"]["mean"])
"""
A B C D
count 5.00 5.00 5.00 5.00
mean 8.00 9.00 10.00 11.00
std 6.32 6.32 6.32 6.32
min 0.00 1.00 2.00 3.00
25% 4.00 5.00 6.00 7.00
50% 8.00 9.00 10.00 11.00
75% 12.00 13.00 14.00 15.00
max 16.00 17.00 18.00 19.00
"""
#列元素个数,行数
# df3=len(df1.index)
# print(df3)
# print(df1.index) #获得索引的列表
#求列数,像字典中key个数
# df4=len(df1.keys())
# print(df4)
# print(df1.keys()) #获得列名的列表
# print(df1.values) #Series数据类型,所有的值元素的矩阵
#所有总的元素个数就是索引数X列数
# print(len(df1.keys())*len(df1.index))
原文地址:https://www.cnblogs.com/yiyea/p/11441803.html