Pandas 是 Python 语言的一个扩展程序库,用于数据分析。
菜鸟教程:https://www.runoob.com/pandas/pandas-tutorial.html
import pandas as pd
data = pd.read_excel("data.xlsx")
print(data)
import pandas as pd
data = pd.read_excel("data.xlsx")
print(data.index.values) # 显示所有行索引:[0 1 2 3 4]
print(len(data.index)) # 显示行数:5
print(data.columns.values) # 显示所有列名:['班级' '序号' '姓名']
print(len(data.columns)) # 显示列数:3
print(data.shape) # 显示元组行列数:(5,3)
import pandas as pd
data = pd.read_excel("data.xlsx")
"""获取列"""
print(data["姓名"]) # 获取单列,类型为Series
print(data["姓名"].values) # 输出:['解叮叮' '许世亮' '王者' '蒋建国' '宋艺然']
print(data[["姓名","班级"]]) # 获取多行,类型为DataFrame
print(data[["姓名","班级"]].values) # 二位列表
print(data.head(2)) # 获取前两行
print(data.tail(2)) # 获取后两行
print(data.sample(2)) # 随机选择2行
"""获取行"""
print(data.loc[0]) # 获取单行,类型为Series
print(data.loc[0].values)
import pandas as pd
data = pd.read_excel("data.xlsx")
"""以下输出均是:解叮叮"""
print(data.loc[0]["姓名"]) # 先行后列
print(data.loc[0,"姓名"])
print(data["姓名"][0]) # 先列后行
print(data.iloc[0,2]) # 选择代表第1行,第3列
import pandas as pd
data = pd.read_excel("data.xlsx")
"""新增、修改行"""
data["班级"] = [2,3,4,5,6] # 修改
data["备注"] = "无" # 新增,值都为“无”
"""新增、修改列"""
data.loc[1] = [2,1002,"嘻嘻嘻"] # 修改
data.loc[len(data.index)] = [6,1006,"哈哈哈"] # 新增
data.loc[len(data.index)] = {"班级":7,"序号":1007,"姓名":"咻咻咻"} # 新增
"""修改单元格的值"""
data.loc[0,"姓名"] = "张三"
data.iloc[1,2] = "李四"
import pandas as pd
data = pd.read_excel("data.xlsx")
"""先获取二维列表格式的值,然后遍历二维列表"""
for row in data.values: # 遍历所有行
for value in row: # 遍历行的每列值
print(value,end=",")
print()
"""根据行索引和列名遍历"""
for row_index in data.index:
for column_name in data.columns:
print(data.loc[row_index, column_name],end=",")
print()
"""根据行列的数字索引遍历"""
for i in range(data.shape[0]):
for j in range(data.shape[1]):
print(data.iloc[i, j],end=",")
print()
import pandas as pd
data = pd.DataFrame({'One': [1, 2, 3]})
data.to_excel('test.xlsx',index=False)
import pandas as pd
data1 = pd.DataFrame({'One': [1, 2, 3]})
data2 = pd.DataFrame({'Two': [4, 5, 6]})
writer = pd.ExcelWriter('test.xlsx')
data1.to_excel(writer, sheet_name='Sheet1', index=False)
data2.to_excel(writer, sheet_name='Sheet2', index=False)
writer.close()
data = pd.read_excel(excel_path)
print(data["班级"].count())
print(data["班级"].mean())
print(data.groupby("班级").agg({'班级': 'count'})) # 以班级分组,显示各个班级人数