Get the App
SLTechnology News&Howtos  ›  Development  › 

How does Python pandas delete specified row / column data

Shulou Source: shulou.com Published: 2022-06-01 21:17:41 10月02日 Update

Python pandas how to delete the specified row / column data, I believe that many inexperienced people do not know what to do, so this article summarizes the causes of the problem and solutions, through this article I hope you can solve this problem.

1. Filter out missing data dropna () import pandas as pdimport numpy as npdf=pd.DataFrame ({"record": [np.nan, "sub-health | Pan Guang | 45 years old", "disease | Zhang Si", np.nan], "date": [np.nan,20210102,20210103,20210104]}, index= ["one", "two", "three", "four"])

1) filter out all lines containing nan values df.dropna () # default axis=0

2) filter out all columns containing nan values df.dropna (axis=1)

3) filter out the row Df.dropna whose elements are nan values (axis=0,how= "all")

4) filter out columns in which all elements are nan values.

5) filter the missing row Df.dropna in the specified column (subset= ["record"], axis=0)

If you need to change the original data directly, you need to set the parameter inplace=True.

two。 Delete the duplicate value drop_duplicates () df=pd.DataFrame ({'state': [1, 1, 1, 2, 2, 2, 2],' pop': ['a, 2, 2, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 2],',

Syntax: drop_duplicates (subset,keep,inplace), where parameter keep: {'first','last',False}, default' first'

First: keep the first duplicates and delete the second and subsequent duplicates.

Last: keep the last duplicates and delete the previous duplicates.

"false": delete all duplicates.

1) keep= "first" df.drop_duplicates (keep= "first")

2) keep= "last" df.drop_duplicates (keep= "last")

3) keep=Falsedf.drop_duplicates (keep=False)

4) delete the row df.drop _ duplicates corresponding to the duplicates in the specified column (subset= ["state"], keep= "first")

If you need to change the original data directly, you need to set the parameter inplace=True.

3. Delete rows and columns drop () df=pd.DataFrame (np.arange (16) .reshape (4p4), columns= ["one", "two", "three", "four"]) according to the specified conditions

1)。 Delete the specified column df.drop (["one"], axis=1)

Alternatively, you can delete a specified column through del df ["one"], but this method is not recommended because it makes changes directly on the source data by default.

2)。 Delete the specified line df.drop ([0], axis=0)

If you need to change the original data directly, you need to set the parameter inplace=True.

After reading the above, have you mastered how Python pandas deletes specified row / column data? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

Tags: Data parameters methods elements content more missing problem helpless to this end sub-health cause this skills conditions disease first time article experience industry Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Docker MariaDB Shulou Technology Apple macOS