怎樣解決python dataframe loc,iloc循環處理速度很慢的問題
1.問題說明
最近用DataFrame做大數據 處理,發現處理速度特別慢,追究原因,發現是循環處理時,loc,iloc速度都特別慢,當數據量特別大得時候真的是超級慢。查很多資料,發現沒有詳細說明,以下為解決辦法
2.問題解決
使用 Pandas.Series.apply 方法,可以對一列數據快速進行處理
Series.apply(*func*, *convert_dtype=True*, *args=()*, **\*kwds*)
函數說明:
To lunch typora from Terminal, you could add
func : function
convert_dtype : boolean, default True
Try to find better dtype for elementwise function results. If False, leave as dtype=object
args : tuple
Positional arguments to pass to function in addition to the value
Additional keyword arguments will be passed as keywords to the function
例子講解
# 首先導入數據
>>> import pandas as pd
>>> import numpy as np
>>> series = pd.