import pandas as pd
import numpy as np
df1 = pd.DataFrame({
"id": ["a1", "a2", np.nan, "a4"],
"age": [20, 23, np.nan, 10],
"sex": ["男", np.nan, np.nan, np.nan],
"time": ["0101", "0202", np.nan, np.nan]
})
print(df1)
print(df1.dropna(how="all")) # 删除所有为空的一行
print(df1.fillna(0)) # 空值填充0
print(df1.fillna({"id": "A", "age": 0, "sex": "-", "time": 0000})) # 比较智能 用字典方式替换需要替换的
df2 = pd.DataFrame({
"id": ["a1", "a2", "a3", "a4", "a4", "a4"],
"age": [20, 23, 26, 10, 10, 10],
"name": ["男", "女", "女", "男", "男", "男"],
"time": ["0101", "0202", "0305", "0506", "0506", "0506"]
})
print(df2)
print(df2.drop_duplicates()) # 删除相同数据 但保留第一个
print(df2["time"].dtype) # 检测数据类型
df2["time"] = df2["time"].astype("string") # 更改标题下的数据类型
print(df2["time"].dtype)