魅族mx5游戏模式小熊猫_熊猫主地图在5分钟内套用和套用
魅族mx5游戲模式小熊貓
Pandas library has two main data structures which are DataFrame and Series. There are many built-in functions to create, manipulate, and analyze these structures.
熊貓庫有兩個主要的數據結構,分別是DataFrame和Series。 有許多內置函數可以創建,操縱和分析這些結構。
In this post, we will master a group of Pandas functions used for manipulating DataFrames and Series. These functions are map, apply, and applymap.
在本文中,我們將掌握一組用于操縱DataFrame和Series的Pandas函數。 這些函數是map , apply和applymap 。
In some cases, Pandas offer better options to use instead of map, apply, and applymap. We will also try to cover these options.
在某些情況下,Pandas提供了更好的選擇來代替map,apply和applymap。 我們還將嘗試介紹這些選項。
Let’s start with the definitions:
讓我們從定義開始:
- Map: Maps (i.e. update, change, modify) the values of a Series. 映射:映射(即更新,更改,修改)系列的值。
- Apply: Applies a function along an axis of a DataFrame. 應用:沿DataFrame的軸應用功能。
- Applymap: Applies a function to a DataFrame element-wise. Applymap:將函數應用于DataFrame元素。
It is important to note that there are cases in which these functions perform the same operation and return the same output. We will see these cases as well as the ones that have differences.
重要的是要注意,在某些情況下,這些功能執行相同的操作并返回相同的輸出。 我們將看到這些情況以及有差異的情況。
One of the major differences is that these functions work on different objects. Applymap works on dataframe whereas map works on series. Apply works on both.
主要區別之一是這些功能可在不同的對象上工作。 Applymap適用于數據框,而map適用于序列。 將作品應用于兩者。
I will first create a simple dataframe to do examples.
我將首先創建一個簡單的數據框來做示例。
import pandas as pdimport numpy as npdf = pd.DataFrame(np.random.randint(10, size=(5,5)), columns=list('ABCDE'))df
Consider we have a function that calculates the average of given values. If we apply this function to the dataframe with “apply”, it will return the averages of rows or columns.
考慮一下我們有一個計算給定值平均值的函數。 如果我們通過“ apply”將此函數應用于數據框,它將返回行或列的平均值。
def calculate_average(row_col):return row_col.mean()
The function is applied along an axis (row or column). Pandas provide functions to perform simple statistical operations on dataframes. In such cases, these functions are preferred over the apply function.
該功能沿軸(行或列)應用。 熊貓提供了對數據框執行簡單統計操作的功能。 在這種情況下,這些功能優于apply功能。
For instance, we can calculate the mean of each column with df.mean(axis=0) and of each row with df.mean(axis=1). However, it is no harm to also know about the apply function.
例如,我們可以使用df.mean(axis=0)計算每一列的平均值,并使用df.mean(axis=1)計算每一行的df.mean(axis=1) 。 但是,知道應用功能也無害。
Applymap and map work on individual elements, not along an axis. To see the difference between map and applymap, let’s do a very simple math operation.
Applymap和map在單個元素上工作,而不是沿著軸。 要查看map和applymap的區別,讓我們做一個非常簡單的數學運算。
Applymap can be applied to the entire dataframe:
Applymap可以應用于整個數據框:
This function can be applied to a row or column using the map function. Thus, the map function cannot be applied to an entire dataframe.
使用map函數可以將此功能應用于行或列。 因此,映射功能不能應用于整個數據幀。
The simple math operations can be done as a vectorized operation which has a simplex syntax and is faster than map or applymap.
簡單的數學運算可以作為向量運算完成,該向量運算具有單一語法,并且比map或applymap更快。
The same goes for the map function:
地圖功能也是如此:
In addition to a function, the map also takes a dictionary or series to map values.
除了功能外,地圖還采用字典或序列來映射值。
Let’s add two new columns to our dataframe:
讓我們在數據框中添加兩個新列:
df['F'] = ['Abc','Dbc','Fcd','Afc','Kcd']df['G'] = ['A',np.nan,'B1','B1','C']df
We want to change the values “B1” in column G as “B”.
我們希望將G列中的值“ B1”更改為“ B”。
As you can see, the other values are mapped NaN which is the standard missing value representation. We also need to specify what other values should be mapped to.
如您所見,其他值被映射為NaN,這是標準的缺失值表示形式。 我們還需要指定其他值應該映射到什么。
Having to also specify the other values makes the map function not an optimal choice in this case. The replace function of Pandas is a better choice here.
在這種情況下,還必須指定其他值會使映射功能不是最佳選擇。 在這里,Pandas的替換功能是一個更好的選擇。
We can also use map and apply to return a list based on the existing values:
我們還可以使用map并應用以基于現有值返回列表:
Each item is a list that contains the original string converted to lowercase letters and the length of the string.
每個項目都是一個列表,其中包含轉換為小寫字母的原始字符串以及該字符串的長度。
These functions work in an iterative fashion which makes them relatively slower. In many cases, vectorized operations or list comprehensions are preferred over using map or applymap.
這些函數以迭代方式工作,這使它們相對較慢。 在許多情況下,矢量化操作或列表推導要比使用map或applymap更好。
Here is a comparison of a list comprehension, for loop, map function on squaring 50000 elements.
這是對平方運算50000個元素的列表理解,循環和映射功能的比較。
There are also some cases in which map function is preferred over a list comprehension. List comprehension loads the entire output list into memory. This is acceptable or even desirable for small or medium-sized lists because it makes the operation faster. However, when we are working with large lists (e.g. 1 billion elements), list comprehension should be avoided. It may cause your computer to crash due to the extreme amount of memory requirement. The map function does not also cause a memory problem.
在某些情況下,地圖功能比列表理解更可取。 列表理解將整個輸出列表加載到內存中。 對于中小型列表,這是可以接受的,甚至是理想的,因為它使操作更快。 但是,當我們處理大型列表(例如10億個元素)時,應避免理解列表。 由于極高的內存需求,可能會導致計算機崩潰。 映射功能也不會引起內存問題。
There is no free lunch! The map function does not cause a memory issue but they’re relatively slower than list comprehensions. Similarly, the speed of list comprehension comes from excessive memory usage. You can decide which one to use depending on your application.
天下沒有免費的午餐! map函數不會引起內存問題,但是它們比列表理解要慢。 同樣,列表理解的速度也來自過多的內存使用。 您可以根據自己的應用程序決定使用哪個。
Thank you for reading. Please let me know if you have any feedback.
感謝您的閱讀。 如果您有任何反饋意見,請告訴我。
翻譯自: https://towardsdatascience.com/master-pandas-map-apply-and-applymap-in-5-minutes-d2bd1bba12a5
魅族mx5游戲模式小熊貓
總結
以上是生活随笔為你收集整理的魅族mx5游戏模式小熊猫_熊猫主地图在5分钟内套用和套用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: JavaScript在表格中实现九九乘法
- 下一篇: python画公主_【图片】来几张公主的