'''
列表:[]
'''
stu = [88, 92, 95] #语文,数学, 英文
# print(stu.count(92))
# print(stu[2] , stu[-1] , stu[1:], stu[-2:])
stu_d = {'chinese':88, 'math':92, 'english':79}
# print(stu['math'], stu['english'])
import pandas as pd
s1 = pd.Series(stu_d)
s2 = pd.Series(stu, index = ['yu', 'shu', 'ying'])
print(s1, type(s1), s1['english'])
print(s2, type(s2), s2['ying'])
print("==========属性==============")
print(s1.index)
for i in s1.index:
print(i)
print(s1.values)
for i in s1.values:
print(i)
for i in s1:
print(i)
print("==========内部函数==============")
print(s1.head(2))
print(s1.tail(2))
print(s1.sum() , len(s1), s1.shape, s1.count())
print("==========排序函数==============")
s1.sort_values(inplace= True, ascending= False)#inplace原地 ascending上升
print(s1)
print("==========二维DataFrame=========")
stu_dd = {'english':[80,90,81,85], 'chinese':[65,75,77,89], 'math':[90,88,85,95]}
df = pd.DataFrame(stu_dd , index=['s1','s2','s3','s4'], columns=['math','chinese', 'english'])
df.insert(0, '性别' , ['男','女', '男', '女'])
df1 = pd.read_excel('pan_15.xlsx') #pd.read_csv('pan_15.csv')
print(df)
print("==========属性===================")
print(df.columns) #Index(['math', 'chinese', 'english'], dtype='object')
print(df.index)#Index(['s1', 's2', 's3', 's4'], dtype='object')
print(df.values)
'''
[[90 65 80]
[88 75 90]
[85 77 81]
[95 89 85]]
'''
print(len(df) ) #print(df.T)
print("======选择=======")
print(df.head(2)) #默认是5行 df.tail(2)
print(df[1:2] ,type(df[1:2]))
print(df.loc['s3'], type(df.loc['s3']))
print(df.iloc[2], type(df.iloc[2]))
#选择 英语 大于等于 85 数学 大于等于 90
cond1 = df['english'] == 90 # df.english == 90
cond2 = df['english'] >= 85
cond4 = (df['english'] >= 85) & (df['math'] >= 90)
cond3 = df['math'] >= 90
#df = df[cond2]
#df = df[cond3]
print(df[cond4])
print("========选取单个的值=========")
print(df.iloc[2, 2])
print(df['english'] ,type(df['english']), df['english']['s2'])
print("========函数=========")
print(df.mean(axis=1) , type(df.sum())) #axis = 0列1行
print(df.describe() , type(df.sum())) #axis = 0列1行
print("========groupby=======")
print(df.groupby('性别' ,as_index =False).mean(), type(df.groupby('性别').mean()))
df = df.groupby('性别' ,as_index =False)['math'].mean()
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['SimHei'] #显示字体
print(df)
plt.title('info score')
plt.bar(df['性别'] , df['math'])
plt.show()
—— 本文来自火龙信奥(义乌睿码科技):义乌青少年信息学奥赛与编程教育平台,专注 CSP-J/S、NOIP、GESP 竞赛培训,线上线下融合教学,助力编程升学。网址:hlcoding.com