Pandas 的 DataFrame.drop(~) 方法返回删除了指定列/行的 DataFrame。
参数
1.labels | string 或 list<string> | optional
要删除的索引或列的名称。
2. axis | int 或 string | optional
是否删除列或行:
|
轴 |
意义 |
|---|---|
|
|
删除行 |
|
|
删除列 |
默认情况下,axis=0 。
3. index | string 或 list<string> | optional
要删除的索引的名称。这相当于指定参数 axis=0 和 labels 。
4. columns | string 或 list<string> | optional
您要删除的列的名称。这相当于指定参数 axis=1 和 labels 。
5. inplace | boolean | optional
-
如果是
True,那么该方法将直接修改源DataFrame,而不是创建新的DataFrame。 -
如果是
False,则将创建并返回一个新的DataFrame。
默认情况下,inplace=False 。
注意
为了清楚起见,指定参数 index 或 columns,而不是指定 labels 和 axis - 代码更短、更清晰。
返回值
删除了指定列/行的 DataFrame。如果 inplace=True ,则返回类型为 None,因为源 DataFrame 被直接修改。
例子
删除列
考虑以下 DataFrame :
df = pd.DataFrame({"A":[1,2], "B":[3,4], "C":[5,6]})
df
A B C
0 1 3 5
1 2 4 6
删除单列
仅删除A列:
df.drop(columns="A")
B C
0 3 5
1 4 6
这相当于以下代码:
df.drop(labels="A", axis=1) # This is not the preferred way since the intention is unclear.
请注意,从 inplace=False 开始,原始的 df 保持不变。
删除多列
要删除列 A 和 B :
df.drop(columns=["A","B"])
C
0 5
1 6
这相当于以下代码:
df.drop(labels=["A","B"], axis=1) # This is not the preferred way since the intention is unclear.
C
0 5
1 6
删除行
考虑以下 DataFrame :
df = pd.DataFrame({"A":[1,2,3], "B":[4,5,6], "C":[7,8,9]})
df
A B C
0 1 4 7
1 2 5 8
2 3 6 9
删除单行
要删除第二行(即带有 index=1 的行):
df.drop(index=1)
A B C
0 1 4 7
2 3 6 9
这相当于以下代码:
df.drop(labels=[1], axis=0) # This is not the preferred way.
A B C
0 1 4 7
2 3 6 9
删除多行
要删除前两行(即带有 index=0 和 index=1 的行):
df.drop(index=[0, 1])
A B C
2 3 6 9
这相当于以下代码:
df.drop(labels=[0,1], axis=0) # This is not the preferred way.
A B C
2 3 6 9
原地掉落
要就地删除行或列,我们需要设置 inplace=True 。其作用是,drop(~) 方法将直接修改源 DataFrame,而不是创建并返回新的 DataFrame。
作为示例,请考虑以下 DataFrame:
df = pd.DataFrame({"A":[1,2], "B":[3,4], "C":[5,6]})
df
A B C
0 1 3 5
1 2 4 6
然后我们用 inplace=True 删除列 A 和 B :
df.drop(columns=["A", "B"], inplace=True)
df
C
0 5
1 6
如输出所示,源 DataFrame 已被修改。
相关用法
- Python PySpark DataFrame drop方法用法及代码示例
- Python PySpark DataFrame dropDuplicates方法用法及代码示例
- Python Pandas DataFrame drop_duplicates方法用法及代码示例
- Python Pandas DataFrame droplevel方法用法及代码示例
- Python PySpark DataFrame dropna方法用法及代码示例
- Python Pandas DataFrame dropna方法用法及代码示例
- Python PySpark DataFrame dtypes属性用法及代码示例
- Python Pandas DataFrame dtypes属性用法及代码示例
- Python Pandas DataFrame duplicated方法用法及代码示例
- Python Pandas DataFrame diff方法用法及代码示例
- Python Pandas DataFrame dot方法用法及代码示例
- Python Pandas DataFrame describe方法用法及代码示例
- Python PySpark DataFrame describe方法用法及代码示例
- Python Pandas DataFrame div方法用法及代码示例
- Python PySpark DataFrame distinct方法用法及代码示例
- Python Pandas DataFrame empty属性用法及代码示例
- Python Pandas DataFrame pop方法用法及代码示例
- Python Pandas DataFrame nsmallest方法用法及代码示例
- Python Pandas DataFrame sample方法用法及代码示例
- Python Pandas DataFrame items方法用法及代码示例
- Python Pandas DataFrame max方法用法及代码示例
- Python Pandas DataFrame swaplevel方法用法及代码示例
- Python Pandas DataFrame agg方法用法及代码示例
- Python Pandas DataFrame copy方法用法及代码示例
- Python Pandas DataFrame pow方法用法及代码示例
注:本文由纯净天空筛选整理自Isshin Inada大神的英文原创作品 Pandas DataFrame | drop method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。
