import pandas as pd
import numpy as np
import random
student01 = pd.read_excel(r"F:\101_pandas视频教程\pandas_excel\Students.xlsx", sheet_name="Page_001")
student02 = pd.read_excel(r"F:\101_pandas视频教程\pandas_excel\Students.xlsx", sheet_name="Page_002")
# 将两张数据表纵向合并
# all_students = pd.concat([student01, student02], axis=1)
all_students = pd.concat([student01, student02]).reset_index(drop=True)
# 向表中追加一列
all_students["age"] = np.arange(0, len(all_students))
# 删除表中的两列
all_students.drop(columns=["Score", "age"], inplace=True)
# 插入一列
all_students.insert(1, column="Foo", value=np.repeat("foo", all_students.shape[0]))
# 更改列名
all_students.rename(columns={"Foo": "FOO", "Name": "name"}, inplace=True)
# 将整数转化为小数,因为只有转化为小数才可以为其设置空值
all_students.ID = all_students["ID"].astype(float)
# 将ID设置几个空值方法一, 注意:前提需先把ID设置为小数类型
# all_students.loc[5:10, "ID"] = all_students.loc[5:10, "ID"].apply(lambda x: np.nan)
# 将ID设置几个空值方法二, 注意:前提需先把ID设置为小数类型
for i in range(5, 10):
all_students["ID"].at[i] = np.nan
# 筛选出ID不为空的行
not_null_students = all_students.loc[all_students["ID"].notna()]
# 筛选出ID为空的行
is_null_students = all_students.loc[all_students["ID"].isna()]
# 在原DataFrame中删除为空的行,无论那一列有np.nan,该行都会被删除掉
all_students.dropna(inplace=True)
# print(not_null_students)
# print(is_null_students)
print(all_students)