03.pandas数据DataFrame

import pandas as pd
#1.
columns=["数学","英语","语文","理科综合","文科综合"]
index=["top2","c9","985","211","1本","2本","3本","大专"]
data={
    "数学":[145,140,135,130,125,120,115,100],
    "英语":[145,140,135,130,125,120,115,100],
    "语文":[145,140,135,130,125,120,115,100],
    "理科综合":[145*2,140*2,135*2,130*2,125*2,120*2,115*2,100*2]
    # "文科综合":[145*2,140*2,135*2,130*2,125*2,120*2,115*2,100*2]
}
frame=pd.DataFrame(data=data,columns=columns,index=index)#创建数据框架
#空缺索引会使用默认数字索引

#2.
columns=["数学","英语","语文","理科综合","文科综合"]
data={
    "数学":[145,140,135,130,125,120,115,100],
    "英语":[145,140,135,130,125,120,115,100],
    "语文":[145,140,135,130,125,120,115,100],
    "理科综合":[145*2,140*2,135*2,130*2,125*2,120*2,115*2,100*2]
    # "文科综合":[145*2,140*2,135*2,130*2,125*2,120*2,115*2,100*2]
}
frame=pd.DataFrame(data=data,columns=columns)#创建数据框架

#一、取出列
print(frame["数学"])
print(frame.数学)#都是series类型
print(frame[["数学","语文"]])#取出多列
#print(frame["数学":"语文"])语文数学必须是数字

#二、取出行 loc可以在[]内填写自定义索引;iloc不行,只能是数字索引
print(frame.ix[0])#取出元素数字索引时取出第一行,其他索引需要把0换成其他
print(frame.loc[0])#取出第一行元素
print(frame.iloc[0])#取出第一行元素
print(frame.ix[0:3])#取出多行
print(frame.loc[["top2","c9"]])
print(frame.loc[0:3])#取出多行,根据自定义索引
print(frame.iloc[0:3])#取出多行,根据数据索引

#三、取出多行多列
print(frame[["数学","语文"]][0:3])#取出语文数学前三行
print(frame[["数学","语文"]].loc[["top2","c9"]])
print(frame[["数学","语文"]].iloc[0:2])

#四、 取出元素
print(frame["数学"][0])#取出元素
print(frame.数学[0])#取出元素
print(frame.iloc[0][0])#取出元素
#一维数组是Serious 多行数组是DataFrame

#3.
columns=["数学","英语","语文","理科综合","文科综合"]
index=["top2","c9","985","211","1本","2本","3本","大专"]
data={
    "数学":[145,140,135,130,125,120,115,100],
    "英语":[145,140,135,130,125,120,115,100],
    "语文":[145,140,135,130,125,120,115,100],
    "理科综合":[145*2,140*2,135*2,130*2,125*2,120*2,115*2,100*2]
    # "文科综合":[145*2,140*2,135*2,130*2,125*2,120*2,115*2,100*2]
}
frame=pd.DataFrame(data=data,columns=columns,index=index)#创建数据框架

  

上一篇:Lending Club 贷款业务评分卡建模


下一篇:【LeetCode】145. Binary Tree Postorder Traversal