pandas行操作

import pandas as pd
import os
#完整显示列
pd.set_option('display.max_columns',None)
#完整显示行
pd.set_option('display.max_rows',None)
os.chdir(r'文件路径')
df1 = pd.read_excel('students.xlsx',sheet_name='page_001')
df2 = pd.read_excel('students.xlsx',sheet_name='page_002')
#drop=True代表删除原来的index
df = df1.append(df2).reset_index(drop=True)
#新建一行series
stu = pd.Series({'ID':41,'Name':'Abel','Score':99})
#插入一行,忽略index
df = df.append(stu,ignore_index=True)
#更改39行的Name为Baily
df.at[39,'Name']='Bailey'
df.at[39,'Score']='100'
#整行替换
stu = pd.Series({'ID':40,'Name':'Bailey','Score':120})
df.iloc[39] = stu
#插入行,先切片
stu = pd.Series({'ID':101,'Name':'Danni','Score':101})
part1 = df[:20]
part2 = df[20:]
df = part1.append(stu,ignore_index=True).append(part2).reset_index(drop=True)
#删除行
# df.drop(index=df[0:10].index,inplace=True)
for i in range(5,15):
    df['Name'].at[i] = ''
#删除空值行
missing = df.loc[df['Name']=='']
df.drop(index=missing.index,inplace=True)
print(df)
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

weixin_50980171

你的鼓励就是对我莫大的支持!

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

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

打赏作者

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

抵扣说明:

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

余额充值