火龙信奥
  • 首页
  • 课程
  • 题库
  • 打卡
    • 代码对战
    • 快速对战
  • 题单
  • 团队
  • 荣誉墙
  • 商城
  • 登录 / 注册

series和dateframe初步11.2

作者: 作者的头像   huolong , 时间:2024-11-02 20:53:19 , 所有人可见, 阅读  9

Series

li = [90,None,99] #语文 数学 英文

dic = {"语文":90 , "数学":89, "英文":99}

print(li[1] , dic['语文'])

import pandas as pd 
s1 = pd.Series(li , index = ['a','b','c'])

print(s1[1] ,s1[0], s1['a'])

print(s1.index, s1.values)

print(s1.head(2), s1.tail(2))

s1['c'] = 97#修改某个值 


s1.sort_values(inplace= True, ascending= False)#inplace原地 ascending上升

print(s1.count())

DataFrame

import pandas as pd 

df1 = pd.read_excel('data.xlsx', engine = 'openpyxl')
print(df1)
print(df1.index, df1.columns , df1.values, len(df1))

print("======获取某一列,它是series")
print(df1['姓名'] , type(df1['学号']))
print(df1['姓名'][1], df1.at[1, "姓名"])

print("-------")
print( df1.iloc[1] , type(df1.iloc[1]) )

print("====")
print(df1.head(1) , type(df1.head(1) ))
#中间的某些行 用切片的方式

print(df1[1:2] , type(df1[1:2]))
print(df1.tail(1) , type(df1.tail(1) ))

print("=====条件过滤===")
cond = (df1['语文'] >= 60 ) & (df1.数学 <= 70)

#筛选两次 或者 使用cond
# df1 = df1[ df1['语文'] >= 60 ]
# df1 = df1[ df1.数学 <= 70 ]

print(df1[ df1.姓名 == 'aad' ]) #根据某个值获取该列 

print(df1[ ['语文', '英文'] ] , type(df1[ ['语文', '英文'] ]))

print("-----常见函数------")


print(df1.英文.count())
#count是用来统计非空的数量
#参数axis = 0 默认是列,1是行
print(df1.count(axis = 1) , type(df1.count()))

#sum求和 axis= 0 列 1行
print(df1.sum(axis=1) , type(df1.sum()))


#sum求和 axis= 0 列 1行
print(df1.mean(axis=1) , type(df1.mean()))

#max求最大值 axis= 0列
print(df1['语文'].max(), type(df1.max()))

#min求最小值 axis = 0列 
print(df1['语文'].min(), type(df1.min()))

# describe()
print(df1.describe() , type(df1.describe()))

print("----")
df1.insert(0, '性别', ['女','女','女','男','男','男','男']) 

print(df1)

#dataframe
print(df1[ ['英文','性别'] ].groupby('性别').mean().at['男', '英文'], type(df1[ ['英文','性别'] ].groupby('性别').mean()))

#Series
print(df1.groupby('性别')['英文'].mean()['男'] , type(df1.groupby('性别')['英文'].mean()))

—— 本文来自火龙信奥(义乌睿码科技):义乌青少年信息学奥赛与编程教育平台,专注 CSP-J/S、NOIP、GESP 竞赛培训,线上线下融合教学,助力编程升学。网址:hlcoding.com

©2026加盟我们 | 关于我们 | ACM课程 | 常见问题 | 成果墙 | 评测记录 | 浙ICP备2021013995号
在线画图 | OI WIki | 打字练习
火龙信奥
请输入登录信息


请完成安全验证
验证码底图 滑块
向右拖动滑块完成验证
请输入用户名 / 绑定的手机号码



请输入注册信息(手机号验证码注册)





验证码5分钟有效,60秒内不可重复获取,每日最多3次

微信登录

微信登录二维码

正在生成二维码...

账号已过期,请续期。
去续期

绑定手机号

📱

为了更好地保护您的账号安全,享受完整的平台服务

请您尽快绑定手机号码