Home > Software design >  Delete Multiple Rows from A Dataset
Delete Multiple Rows from A Dataset

Time:10-06

I want to delete multiple rows where, df.service == 'harvest' or 'http_2873' or 'red_i', but I'm running a for loop and going through the entire df instead

for _ in df1.service:
    i = df1[((df1.service == 'harvest'))].index
    df1.drop(i)

CodePudding user response:

Use this:

df[~((df['Service'] == 'harvest') | (df['Service'] == 'http_2873') | (df['Service'] == 'red_i'))]

.

df
    Date        Customers   Location    Sales   Service
0   2021-10-05  A           NSW         12      harvest
1   2021-10-03  B           NSW         10      http_2873
2   2021-10-01  C           NSW         33      red_i
0   2021-10-05  A           NSW         12      harvest
1   2021-10-03  B           NSW         10      http_2873
2   2021-10-01  C           NSW         33      red_i
0   2021-10-05  A           NSW         12      harvest
1   2021-10-03  B           NSW         10      http_2873
2   2021-10-01  C           NSW         33      red_i
0   2021-10-05  A           NSW         12      harvest
1   2021-10-03  B           NSW         10      http_2873
2   2021-10-01  C           NSW         33      red_i

df[~((df['Service'] == 'harvest') | (df['Service'] == 'http_2873') | (df['Service'] == 'red_i'))]

    Date        Customers   Location    Sales   Service
1   2021-10-03  B           NSW         10      hello
2   2021-10-01  C           NSW         33      hello1
0   2021-10-05  A           NSW         12      hello3

CodePudding user response:

df2 = df[(df['service'] != 'harvest') & (df['service'] != 'http_2873') & (df['service'] != 'red_i')]

  • Related