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

What are the methods of using Pandas and NumPy functions

2025-04-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly explains "what are the methods of using Pandas and NumPy functions". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let the editor take you to learn "what are the ways to use Pandas and NumPy functions"?

1. Argpartition ()

NumPy has this amazing ability to find N maximum indexes. The output will be N maximum indexes, and the values can then be sorted as needed.

X = np.array ([12, 10, 12, 0, 6, 8, 9, 1, 16, 4, 6, 0]) index_val = np.argpartition (x,-4) [- 4:] index_val array ([1,8,2,0], dtype=int64) np.sort (x [index _ val]) array ([10,12,12,16])

2. Allclose ()

Allclose () is used to match two arrays and get the output as a Boolean value. If the items in the two arrays are not equal within the tolerance, False is returned. This is a good way to check whether two arrays are similar, which is actually very difficult to do manually.

Array1 = np.array ([0.12 array1,array2,0.1 0.17 False 0.24 with a tolerance of 0.29]) array2 = np.array ([0.13 record0.19 0.26 meme 0.31]) # with a tolerance of 0.1, it should return False: np.allclose (array1,array2,0.1) False # with a tolerance of 0.2, it should return True: np.allclose (array1,array2,0.2) True

3. Clip ()

Clip () is used to keep values in an array within an interval. Sometimes we need to keep the value within the upper and lower limits. For the above purposes, we can use NumPy's clip (). Given an interval, values outside the interval are clipped to the edge of the interval.

X = np.array ([3, 17, 14, 23, 2, 2, 6, 8, 1, 2, 16, 0]) np.clip (XL2) array ([3, 5, 5, 5, 2, 2, 2))

4. Extract ()

As the name implies, Extract () is used to extract specific elements from an array based on specific conditions. With extract (), we can also use conditions such as and and or.

# Random integers array = np.random.randint (20, size=12) array array ([0, 1, 8, 19, 16, 18, 10, 11, 2, 13, 14, 3]) # Divide by 2 and check if remainder is 1 cond = np.mod (array, 2) = 1 cond array ([False, True, False, True]) # Use extract to get the values np.extract (cond, array) array ([1,19,11,13) 3]) # Apply condition on extract directly np.extract ((array)

< 3) | (array >

15), array) array ([0, 1, 19, 16, 18, 2])

5. Where ()

Where () is used to return elements from an array that meets certain conditions. It returns the index position of the value under certain conditions. This is almost similar to the where condition we used in SQL, which I'll demonstrate in the following example.

Y = np.array ([1Miss', 5, Hit 6]) # Where y is greater than 5, returns index position np.where (y > 5) array ([2,3,5,7,8], dtype=int64),) # First will replace the values that match the condition, # second will replace the values that does not np.where (y > 5, "Hit", "Miss") array (['Miss',' Miss', 'Hit',' Hit', 'Miss',' Hit', 'Miss') 'Hit',' Hit'], dtype='

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