DataFrame的结构为二维。创建方法类似字典,由列名:Series组成。
创建DataFrame示例1:

import pandas as pd
s_id = pd.Series(["01", "02", "03", "04", "05"])
s_class = pd.Series(["二班", "一班", "二班", "三班", "一班"])
s_grade = pd.Series([92, 67, 70, 88, 76])

df1 = pd.DataFrame({"学号": s_id, "班级": s_class, "成绩": s_grade})

示例2:

df4 = pd.DataFrame({"学号": {"小明": "01", "小红": "02", "小杰": "03", 
                           "小丽": "04", "小华": "05"}, 
                    "班级": {"小明": "二班", "小红": "一班", "小杰": "二班", 
                           "小丽": "三班", "小华": "一班"},
                    "成绩": {"小明": 92, "小红": 67, "小杰": 70, 
                           "小丽": 88, "小华": 76}})
df4

常用示例:

df4.index 行标签
df4.columns 列标签
df4.values 值
df4.T 转置
df4.班级/df4["班级"] 均为取列值
df4[["学号","成绩"]] 取多列需要用到列表
df4.loc[["小红","小杰"],"班级":"成绩"]

筛选:

df4[(df4["成绩"] > 70) & (df4["班级"] == "二班")]

取前几列:
df4.head(n)

posted on 2024-01-25 15:11  我是1只鸟  阅读(4)  评论(0编辑  收藏  举报