网络编程
位置:首页>> 网络编程>> 网络编程>> Pandas:Series和DataFrame删除指定轴上数据的方法

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

作者:BQW_  发布时间:2022-12-22 12:49:26 

标签:Pandas,Series,DataFrame

如下所示:


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 草莓

来源:https://blog.csdn.net/bqw18744018044/article/details/79935443

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com