pandas中dataframe中列的操作方法和空行的操作方法

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)

 

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

专职

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值