【问题标题】:How inplace attribute works in python? [duplicate]python中的inplace属性如何工作? [复制]
【发布时间】:2021-05-10 10:09:50
【问题描述】:
import pandas as pd
s=pd.Series(data=[45,20,35,50,60],index=['a','b','c','d','e'])
s.drop("a",inplace=False)
print(s)
a    45
b    20
c    35
d    50
e    60
dtype: int64

s.drop("a",inplace=True)
    b    20
    c    35
    d    50
    e    60
    dtype: int64

当我将 inplace 属性的值更改为 False 时,索引“a”处的元素不会被删除,但是当我更改 inplace 的值时 = True 索引“a”处的值会被删除。我不明白它是如何工作的。

【问题讨论】:

    标签: python pandas


    【解决方案1】:

    当您使用inplace=False 调用drop 时,drop 将返回一个新系列,而不是在现有系列中删除请求的行。换句话说:

    x = s.drop("a",inplace=False)
    print(s)
    print()
    print(x)
    

    产生:

    a    45
    b    20
    c    35
    d    50
    e    60
    dtype: int64
    
    b    20
    c    35
    d    50
    e    60
    dtype: int64
    

    地点:

    x = s.drop("a",inplace=True)
    print(s)
    print()
    print(x)
    

    产生:

    b    20
    c    35
    d    50
    e    60
    dtype: int64
    
    None
    

    【讨论】:

      猜你喜欢
      • 2013-10-09
      • 1970-01-01
      • 1970-01-01
      • 2011-07-08
      • 1970-01-01
      • 2023-03-25
      • 2015-08-21
      相关资源
      最近更新 更多