请问一下大家,pandas怎么根据条件只删除第一个满足该条件的行,而不是都删除呀?
1条回答 默认 最新
- ENGineer_zlshuo 2022-05-06 12:34关注
import pandas as pd """原内容 num time 0 1 6 1 2 7 2 3 8 3 4 1 """ df = pd.read_excel('test.xlsx', sheet_name = 'Sheet1') # 读取对应的表单 rows = df.loc[df['time'].astype(int) > 5].index # 筛选出所有满足条件的行索引,比如time列值大于5的行 df.drop(index = rows[0], inplace = True) # 只删除第一行 print(df) """结果 num time 1 2 7 2 3 8 3 4 1 """ df.reset_index(drop = True, inplace = True) # 重置索引 print(df) """结果 num time 0 2 7 1 3 8 2 4 1 """
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报 编辑记录