相思本是无凭语,

莫向花牋费泪行。

hanstary

相思本是无凭语,莫向花牋费泪行。

Pandas基本操作

Pandas基本操作

1.Series

(1)Series的创建

pd.Series(data,index=列表)
import pandas as pd

se = pd.Series(["张三", "李四", "王五"], index=["stu1", "stu2", "stu3"])
print(se)

也可以用字典创建

import pandas as pd

dic = {
    "sud1": "张三",
    "stu2": "李四",
    "stu3": "王五"
}
se = pd.Series(dic)
print(se)

字典的key就会变成行名,字典的value就会变成数据

(2)Series的属性

属性 说明
index 行名
values 数据
import pandas as pd

dic = {
    "sud1": "张三",
    "stu2": "李四",
    "stu3": "王五"
}
se = pd.Series(dic)
print("行名")
print(se.index)
print("数值")
print(se.values)

(3)取值

se.loc[行名]
import pandas as pd

dic = {
    "sud1": "张三",
    "stu2": "李四",
    "stu3": "王五"
}
se = pd.Series(dic)
print(se.loc["sud1"])

2.DataFrame

(1)DataFrame的创建

pd.DataFrame(data,index=列表,columns=列表)
import pandas as pd

data = [
    ["张三", "男", "18"],
    ["李四", "男", "19"],
    ["王五", "男", "20"]
]
df = pd.DataFrame(data, index=["stu1", "stu2", "stu3"], columns=["姓名", "性别", "年龄"])
print(df)

字典创建

import pandas as pd

data = [
    {"姓名": "张三", "性别": "男", "年龄": 18},
    {"姓名": "李四", "性别": "男", "年龄": 19},
    {"姓名": "王五", "性别": "男", "年龄": 20}
]
df = pd.DataFrame(data, index=["stu1", "stu2", "stu3"])
print(df)

或者

import pandas as pd

data = {
    "姓名": ["张三", "李四", "王五"],
    "性别": ["男", "男", "男"],
    "年龄": [18, 19, 20]
}
df = pd.DataFrame(data, index=["stu1", "stu2", "stu3"])
print(df)

(2)DataFrame的属性

属性 说明
dtypes DataFrame的类型
values DataFrame的数据
index 行名
columns 列名
shape 形状
size 元素个数
import pandas as pd

data = {
    "姓名": ["张三", "李四", "王五"],
    "性别": ["男", "男", "男"],
    "年龄": [18, 19, 20]
}
df = pd.DataFrame(data, index=["stu1", "stu2", "stu3"])
print("dtypes")
print(df.dtypes)
print("values")
print(df.values)
print("index")
print(df.index)
print("columns")
print(df.columns)
print("shape")
print(df.shape)
print("size")
print(df.size)

(3)对齐输出

pd.set_option("display.unicode.east_asian_width",True)

(4)数据操作

A.查

df.loc[行名,列名]
df.iloc[行下标,列下标]
import pandas as pd

data = {
    "姓名": ["张三", "李四", "王五"],
    "性别": ["男", "男", "男"],
    "年龄": [18, 19, 20]
}
df = pd.DataFrame(data, index=["stu1", "stu2", "stu3"])
# 获得单个数据
print(df.loc["stu2", "姓名"])
# 获得行数据
print(df.loc["stu1", :])
# 获得列数据
print(df.loc[:, "年龄"])
# 获得多个数据
print(df.loc[["stu1", "stu2"], :])
print(df.loc[:, ["姓名", "年龄"]])

B.改

df.loc[行名,列名]=新值
df.iloc[行下标,列下标]=新值
import pandas as pd

data = {
    "姓名": ["张三", "李四", "王五"],
    "性别": ["男", "男", "男"],
    "年龄": [18, 19, 20]
}
df = pd.DataFrame(data, index=["stu1", "stu2", "stu3"])
df.loc["stu1", "年龄"] = 25
print(df)

C.增

# 添加行数据
df.loc[行名]=值
df.iloc[行下标]=值
#添加列数据
df.loc[:,列名]=值
df.iloc[:,列下标]=值
df[列名]=值
import pandas as pd

data = {
    "姓名": ["张三", "李四", "王五"],
    "性别": ["男", "男", "男"],
    "年龄": [18, 19, 20]
}
df = pd.DataFrame(data, index=["stu1", "stu2", "stu3"])
# 显示的对齐
pd.set_option("display.unicode.east_asian_width",True)
# 插入行
df.loc["stu4"] = ["赵六", "男", 21]
# 插入列
df.loc[:, "爱好"] = ["唱", "跳", "rap", "篮球"]
print(df)

D.删

# 删除行数据
df.drop(行名或列名,axis=0)
# 删除列数据
df.drop(行名或列名,axis=1)
import pandas as pd

data = {
    "姓名": ["张三", "李四", "王五"],
    "性别": ["男", "男", "男"],
    "年龄": [18, 19, 20]
}
df = pd.DataFrame(data, index=["stu1", "stu2", "stu3"])
# 显示的对齐
pd.set_option("display.unicode.east_asian_width", True)
# 删除行
df = df.drop("stu1", axis=0)
# 删除列
df = df.drop("性别", axis=1)
print(df)

(5)遍历行列

# 遍历行
for key,value in df.iterrows():
    ...

# 遍历列
for key,value in df.items():
    ...
import pandas as pd

data = {
    "姓名": ["张三", "李四", "王五"],
    "性别": ["男", "男", "男"],
    "年龄": [18, 19, 20]
}
df = pd.DataFrame(data, index=["stu1", "stu2", "stu3"])
# 显示的对齐
pd.set_option("display.unicode.east_asian_width", True)
# 遍历行
for key, value in df.iterrows():
    print(key)
    print(value)
# 遍历列
print("====================================")
for key, value in df.items():
    print(key)
    print(value)

posted on 2024-06-30 10:05  hanstary  阅读(8)  评论(0编辑  收藏  举报

导航