Home > Back-end >  How to drop multiple rows of a pandas df at once?
How to drop multiple rows of a pandas df at once?

Time:03-23

I have a dataframe with 198 rows and 60 columns. I need to delete about 20 of these rows, is there any way to do this quickly?

df.drop would not allow me to

CodePudding user response:

You can try this:

data = np.random.randint(100, size=(10,10))
df = pd.DataFrame(data)
print(df)

Before using df.drop():

   0   1   2   3   4   5   6   7   8   9
0  87  36  28  25  10  28  99  54  45  36
1  96  25  64  30  47  60  65  69  78  40
2  64  29  65  49  50  99  11  89  52  33
3  96  68  98  41  37  94  21  90  74  68
4  87  23  67  50  76  85  63  37  91  71
5  50   4  60  62  72  76  61  11  93  30
6  21  18  62  34  15  72  85  31  62  66
7  57  18  40  25  10  30  35  62  73  43
8   1  89  75  25  84  11  82  36  98  58
9  78  49  46  52   8  84   2  29  57  87

print()

After using df.drop():

print(df.drop([0,1,5,9]))

    0   1   2   3   4   5   6   7   8   9
2  64  29  65  49  50  99  11  89  52  33
3  96  68  98  41  37  94  21  90  74  68
4  87  23  67  50  76  85  63  37  91  71
5  50   4  60  62  72  76  61  11  93  30
6  21  18  62  34  15  72  85  31  62  66
7  57  18  40  25  10  30  35  62  73  43
8   1  89  75  25  84  11  82  36  98  58
9  78  49  46  52   8  84   2  29  57  87

CodePudding user response:

Deleting rows or columns? What do you mean it would allow you to delete?

# Delete Rows by Index Range
df1=df.drop(df.index[2:])

Are you looking for a filter condition among the data? Like using window partition function here

  • Related