python

超轻量级php框架startmvc

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

更新时间:2020-06-11 20:36:01 作者:startmvc
如下所示:importnumpyasnpimportpandasaspdfrompandasimportSeries,DataFrame一、drop方法:产生新对象1.Series

如下所示:


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删除指定轴上数据的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。

Pandas Series DataFrame