In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
Today, I will talk to you about how to delete DataFrame data in Pandas. Many people may not know much about it. In order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.
Next, we introduce the deletion of DataFrame data in Pandas, mainly using drop and del methods.
The argument of # drop function explains drop (self, labels=None, # is the label of the row and column to be deleted, given in the list; axis=0, # axis refers to which axis, 0 is the row (default) and 1 is the column Index=None, # index refers to a row or rows of columns=None, # columns refers to a column or columns of level=None, # level refers to a level, for the case of multiple indexes; inplace=False, whether # inplaces replaces the original dataframe; errors= "raise",) axis=0 or rows specified by index or columns only need to use a set of 1. Based on the default row-column index operation
Sample data
Import numpy as npimport pandas as pd# generates a random array-5 rows and 5 columns df = pd.DataFrame (np.random.rand (5)) print (df)
Data presentation
0 12 3 40 0.760489 0.074633 0.788416 0.087612 0.5605391 0.758450 0.599777 0.384075 0.525483 0.6289102 0.386808 0.148106 0.742207 0.452627 0.7759633 0.662909 0.134640 0.186186 0.735429 0.4595564 0.328694 0.269088 0.331404 0.835388 0.8991071.1 row deletion
[1] Delete a single line
# Delete a single line, delete line 2 df.drop (df.index [1], inplace=True) # inplace=True modify print (df)
Execution result:
0 1 2 3 4
0 0.605764 0.234973 0.566346 0.598105 0.478153
2 0.383230 0.822174 0.228855 0.743258 0.076701
3 0.875287 0.576668 0.176982 0.341827 0.112582
4 0.205425 0.898544 0.799174 0.000905 0.377990
[2] Delete discontiguous lines
# Delete discontiguous lines, delete lines 2 and 4 df.drop (df.index [[1d3]], inplace=True) print (df)
Execution result:
0 1 2 3 4
0 0.978612 0.556539 0.781362 0.547527 0.706686
2 0.845822 0.321716 0.444176 0.053915 0.296631
4 0.617735 0.040859 0.129235 0.525116 0.005357
[3] Delete consecutive lines
# Delete consecutive multiline df.drop (df.index [1:3], inplace=True) # open range, and the last index number is not counted as print (df)
Execution result:
0 1 2 3 4
0 0.072891 0.926297 0.882265 0.971368 0.567840
3 0.163212 0.546069 0.360990 0.494274 0.065744
4 0.752917 0.242112 0.526675 0.918713 0.320725
1.2 column deletion
Del and drop can be used to delete the column. Del df [1] # deletes the second column, which is deleted in place. This article specifically explains the deletion of the drop function.
[1] Delete the specified column
Df.drop ([1p3], axis=1,inplace=True) # specify the axis as the column # df.drop (columns= [1p3], inplace=True) # specify the column directly
Execution result:
0 2 4
0 0.592869 0.123369 0.815126
1 0.127064 0.093994 0.332790
2 0.411560 0.118753 0.143854
3 0.965317 0.267740 0.349927
4 0.688604 0.699658 0.932645
[2] Delete consecutive columns
Df.drop (df.columns [1:3], axis=1,inplace=True) # specify axis # df.drop (columns=df.columns [1:3], inplace=True) # specify column print (df)
Execution result:
0 3 4
0 0.309674 0.974694 0.660285
1 0.677328 0.969440 0.953452
2 0.954114 0.953569 0.959771
3 0.365643 0.417065 0.951372
4 0.733081 0.880914 0.804032
two。 According to the custom row and column index operation
Sample data
Df = pd.DataFrame (data=np.random.rand (5)) df.index = list ('abcde') df.columns = list (' one two three four five') print (df)
Data presentation
One two three four five a 0.188495 0.574422 0.530326 0.842489 0.474946b 0.912522 0.982093 0.964031 0.498638 0.826693c 0.580789 0.013957 0.515229 0.795052 0.859267d 0.559267d 0.565602 0.305256 0.552566 0.754791e 0.375407 0.236118 0.129210 0.711744 0.0673562.1 row deletion
[1] Delete a single line
Df.drop (['b'], inplace=True) print (df)
Execution result:
One, two, three, four, five.
A 0.306350 0.622067 0.030573 0.490563 0.009987
C 0.672423 0.071661 0.274529 0.400086 0.263024
D 0.654204 0.809087 0.066099 0.167290 0.534452
E 0.628917 0.232629 0.070167 0.469962 0.957898
[2] Delete multiple lines
Df.drop (['baked Magazine], inplace=True) print (df)
Execution result:
One, two, three, four, five.
A 0.391583 0.509862 0.924634 0.466563 0.058414
C 0.802016 0.621347 0.659215 0.575728 0.935811
E 0.223372 0.286116 0.130587 0.113544 0.910859
2.2 column deletion
[1] Delete a single column
Df.drop (['II'], axis=1,inplace=True) # Delete single column print (df)
Execution result:
One, three, four, five.
A 0.276147 0.797404 0.184472 0.081162
B 0.630190 0.328055 0.428668 0.168491
C 0.979958 0.029032 0.934626 0.106805
D 0.762995 0.003134 0.136252 0.317423
E 0.137211 0.116607 0.367742 0.840080
[2] Delete multiple columns
Df.drop (['2','4'], axis=1,inplace=True) # Delete multi-column # df.drop (columns= ['2','4'], inplace=True) # delete multi-column print (df)
Execution result:
One, three, five.
A 0.665647 0.709243 0.019711
B 0.920729 0.995913 0.490998
C 0.352816 0.185802 0.406174
D 0.136414 0.563546 0.762806
E 0.259710 0.775422 0.794880
After reading the above, do you have any further understanding of how to delete DataFrame data in Pandas? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.