[英]Drop row in a Pandas Dataframe by using index value and index position
Unable to drop a specific row based on the index value and its index position.无法根据索引值及其索引 position 删除特定行。 For my system it is crucial to use both.
对于我的系统来说,两者都使用是至关重要的。 Code below.
代码如下。
Current Dataframe:当前 Dataframe:
TICKER ORDER ID BUY DATE
TMC 1 1
TMC 1 1
TMC 1 1
TMC 2 1
RVPH 1 1
TSLA 150 09/18/2022, 18:10:13
TMC 1 1
TMC 1 1
Willing to drop the row with index value == 'TMC', counting three from bottom of the dataframe.愿意删除索引值 == 'TMC' 的行,从 dataframe 底部算起三个。 It is for purposes of appending data and making it easier for me to modify and format.
这是为了附加数据并使我更容易修改和格式化。
This is what I have tried but gives error: ExcelPD = ExcelPD.drop(ExcelPD.loc['TMC'].iloc[-3])这是我尝试过但给出错误的方法: ExcelPD = ExcelPD.drop(ExcelPD.loc['TMC'].iloc[-3])
Error: line 4340, in _drop_axis raise KeyError(f"{labels} not found in axis")错误:第 4340 行,在 _drop_axis 中引发 KeyError(f"{labels} not found in axis")
ExcelPD.loc['TMC']
selects the TMC
row, you can use boolean indexing ExcelPD.loc['TMC']
选择TMC
行,可以使用boolean索引
# if `TMC` is in index
ExcelPD = ExcelPD[ExcelPD.index != 'TMC']
# if `TMC` in in column
ExcelPD = ExcelPD[ExcelPD['TICKER'] != 'TMC']
# or
ExcelPD = ExcelPD.query("TICKER != 'TMC'")
Or use drop
if TMC
is in index或者如果
TMC
在索引中,则使用drop
ExcelPD = ExcelPD.drop(index='TMC')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.