Pandas:Series和DataFrame删除指定轴上数据 Pandas:Series和DataFrame删除指定轴上数据的方法

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

Pandas:Series和DataFrame删除指定轴上数据 Pandas:Series和DataFrame删除指定轴上数据的方法

BQW_   2021-03-30 我要评论

如下所示:

import numpy as np
import pandas as pd
from pandas import Series,DataFrame

一、drop方法:产生新对象

1.Series

o = Series([1,3,4,7],index=['d','c','b','a'])
print(o.drop(['d','b']))
c  3
a  7
dtype: int64

2.DataFrame

data = {'水果':['苹果','梨','草莓'],
    '数量':[3,2,5],
    '价格':[10,9,8]}
df = DataFrame(data)
print(df)
  价格 数量 水果
0 10  3 苹果
1  9  2  梨
2  8  5 草莓

删除第0轴(行)

print(df.drop([0,2]))
  价格 数量 水果
1  9  2 梨

删除第1轴(列)

print(df.drop(['价格','数量'],axis=1))
  水果
0 苹果
1  梨
2 草莓

二、del关键字:在原对象上删除

1.Series

del o['a']
print(o)
d  1
c  3
b  4
dtype: int64

2.DataFrame

del df['价格']
print(df)
  数量 水果
0  3 苹果
1  2  梨
2  5 草莓

以上这篇Pandas:Series和DataFrame删除指定轴上数据的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。

猜您喜欢

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们