import pandas as pd if __name__ == '__main__': page_1 = pd.read_excel("C:/Users/18124/Desktop/pandas/027_行操作/Students.xlsx", sheet_name="Page_001") page_2 = pd.read_excel("C:/Users/18124/Desktop/pandas/027_行操作/Students.xlsx", sheet_name="Page_002") # print(page_1.head()) # print(page_2.head()) # 复制数据 - 重新刷新id students = page_1.append(page_2).reset_index(drop=True) print(students) # 添加数据 stu_add_v1 = pd.Series([41, "jack", 88], index=["ID", "Name", "Score"]) stu_add_v2 = pd.Series({"ID":42, "Name":"test_name", "Score":98}) stu_pd = students.append(stu_add_v1, ignore_index=True) # 添加数据 - 自动分配index stu_pd = stu_pd.append(stu_add_v2, ignore_index=True) # 添加数据 - 自动分配index print(stu_pd) # 修改数据 :单数据 stu_pd["Name"].at[40] = "aa" stu_pd.at[40, "Score"] = "98" # 修改数据 : 整行修改 stu_add_v3 = pd.Series({"ID":42, "Name":"bb", "Score":88}) stu_pd.iloc[41] = stu_add_v3 print(stu_pd) # 插入数据 # 插入位置 : 索引位置 - 第20行 stu_add_v4 = pd.Series({"ID": 8888, "Name": "888", "Score": 888}) part1 = stu_pd[:20] part2 = stu_pd[20:] students = part1.append(stu_add_v4, ignore_index=True).append(part2).reset_index(drop=True) print(students) # # 删除数据 students.drop(index=[0], inplace=True) # 单条数据 students.drop(index=[1, 2], inplace=True) # 多条数据 students.drop(index=range(11, 20), inplace=True) # 多条数据 students.reset_index(inplace=True, drop=True) print(students) students.drop(index=students[10:20].index, inplace=True) # 多条数据 students.reset_index(inplace=True, drop=True) print(students) # 删除数据 - 条件过滤 stu_88 = students.loc[students["Score"] == 80] students.drop(index=stu_88.index, inplace=True) students.reset_index(inplace=True, drop=True) print(students)