Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use pandas of Python

2025-03-06 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

In this article, the editor introduces in detail "how to use Python's pandas". The content is detailed, the steps are clear, and the details are handled properly. I hope this article "how to use Python's pandas" can help you solve your doubts.

1. Isnull ()

Isnull () is used to find out the location of the missing value and returns a Boolean type mask tag missing value. Here is an example:

Import pandas as pd

Import numpy as np

Data = pd.DataFrame ({'name': [' W3CSCHOOLING]], 'age': [18pnp.None])

Data

Execute the above code to get the data as follows:

Name age

0 W3CSCHOOL 18.0

1 NaN NaN

2 JAVA 99.0

3 PYTHON NaN

Here we can see that no matter whether we use np.nan or None when we create DataFrame, it will become NaN after creation.

Data.isnull ()

Name age

0 False False

1 True True

2 False False

3 False True

2. Notnull ()

Notnull () is the opposite of isnull (), which finds a non-null value and marks it with a Boolean value. Here is an example:

Data.notnull ()

Name age

0 True True

1 False False

2 True True

3 True False

3. Dropna ()

Dropna () means literally, losing the missing value.

DataFrame.dropna (axis=0, how='any', thresh=None, subset=None, inplace=False)

Parameters:

Axis: default is 0, which indicates whether to delete a row or a column. You can also use "index" and "columns".

How: {'any',' all'}. The default 'any';any means to delete the entire row (column) as long as the row (column) has a null value. All means that the entire row (column) will be deleted only if the whole row (column) has a null value.

Thresh: delete if the non-null value of deletion is less than the number of thresh

Subset: list type, indicating which columns have null values before rows or columns are deleted

Inplace: like the inplace of other functions, indicates whether to overwrite the original DataFrame.

Here is an example:

Data.dropna (axis=1,thresh=3)

Name

0 W3CSCHOOL

1 NaN

2 JAVA

3 PYTHON

Data.dropna (axis=0,how='all')

Name age

0 W3CSCHOOL 18.0

2 JAVA 21.0

3 PYTHON NaN

Data.dropna (subset = ['name'])

Name age

0 W3CSCHOOL 18.0

2 JAVA 21.0

3 PYTHON NaN

4. Fillna ()

The function of fillna () is to fill in the missing values

DataFrame.fillna (value=None, method=None, axis=None, inplace=False, limit=None, downcast=None)

Parameters:

Value: sets the value used to populate DataFrame

Method: defaults to None;. There are four ways to fill DataFrame: 'backfill',' bfill',-'pad',' ffill', in which 'backfill' and' bfill' fill the blank values with the previous values, and 'pad' and' ffill' fill the vacant values with the latter values.

Axis: fill the axis along which the missing values are filled, as in the axis setting method above

Inplace: whether to replace the original DataFrame, in the same way as above

Limit: sets the limit on the number of values to be replaced

Downcast: indicates backward compatible conversion types, which are not commonly used

Here is an example:

Data.fillna (0)

Name age

0 W3CSCHOOL 18.0

1 0 0.0

2 JAVA 21.0

3 PYTHON 0.0

Data.fillna (method='ffill')

Name age

0 W3CSCHOOL 18.0

1 W3CSCHOOL 18.0

2 JAVA 21.0

3 PYTHON 21.0

After reading this, the article "how to use Python's pandas" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it yourself. If you want to know more about related articles, welcome to follow the industry information channel.

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report