Pandas DataFrame.floordiv(~)方法將源DataFrame中的值除以標量、序列、係列或DataFrame整數除法, 那是:
DataFrame // other注意
除非您使用參數 axis 、 level 和 fill_value ,否則 floordiv(~) 相當於使用 // 運算符執行除法。
參數
1.other | scalar 或 sequence 或 Series 或 DataFrame
結果DataFrame 將是源DataFrame 通過整數除法除以other。
2. axis | int 或 string | optional
是否為源DataFrame的每一列或每一行廣播other:
| 軸 | 說明 | 
|---|---|
| 
 | 每列廣播 | 
| 
 | 每行廣播  | 
請注意,這僅在源 DataFrame 的形狀與 other 的形狀不匹配時才相關。默認情況下,axis=1 。
3. level | int 或 string | optional
要考慮的級別的名稱或整數索引。僅當您的 DataFrame 是多索引時,這才相關。
4. fill_value | float 或 None | optional
在計算之前替換NaN的值。請注意,如果兩對條目均為 NaN ,則結果仍為 NaN 。默認情況下,fill_value=None 。
返回值
整數除法產生新的DataFrame。
例子
基本用法
考慮以下 DataFrame :
df = pd.DataFrame({"A":[5,6], "B":[7,8]})
df_other = pd.DataFrame({"A":[1,2], "B":[3,4]})
   A  B  |     A  B
0  5  7  |  0  1  3  
1  6  8  |  1  2  4執行整數除法:
df.floordiv(df_other)
   A  B
0  5  2
1  3  2在這裏,我們執行以下逐元素樓層劃分:
5//1 7//3
6//2 8//4請注意,這相當於:
df // df_other
   A  B
0  5  2
1  3  2廣播
考慮以下 DataFrame :
df = pd.DataFrame({"A":[3,4], "B":[5,6]})
df
   A  B
0  3  5
1  4  6逐行整數除法
默認情況下, axis=1 ,這意味著 other 將為 df 中的每一行廣播:
df.floordiv([1,2])   # axis=1
   A  B
0  3  2
1  4  3在這裏,我們進行以下逐元素整數除法:
3//1 5//2
4//1 6//2按列整數除法
要為 df 中的每一列廣播 other,請像這樣設置 axis=0:
df.floordiv([1,2], axis=0)
   A  B
0  3  5
1  2  3在這裏,我們進行以下按元素劃分:
3//1 5//1
4//2 6//2指定fill_value
考慮以下數據幀:
df = pd.DataFrame({"A":[6,np.NaN], "B":[np.NaN,7]})
df_other = pd.DataFrame({"A":[2,3], "B":[np.NaN,np.NaN]})
   A    B    |     A  B
0  6.0  NaN  |  0  2  NaN
1  NaN  7.0  |  1  3  NaN默認情況下,當我們使用 floordiv(~) 執行整數除法時,任何使用 NaN 的操作都會得到 NaN :
df.floordiv(df_other)
   A    B
0  3.0  NaN
1  NaN  NaN我們可以在使用fill_value參數執行整數除法之前填充NaN值:
df.floordiv(df_other, fill_value=1)
   A    B
0  3.0  NaN
1  0.0  7.0請注意,如果整數除法在兩個 NaN 之間,則結果將始終是 NaN,無論 fill_value 如何。
相關用法
- Python PySpark DataFrame filter方法用法及代碼示例
- Python Pandas DataFrame first_valid_index方法用法及代碼示例
- Python Pandas DataFrame filter方法用法及代碼示例
- Python Pandas DataFrame first方法用法及代碼示例
- Python Pandas DataFrame fillna方法用法及代碼示例
- Python PySpark DataFrame foreach方法用法及代碼示例
- Python PySpark DataFrame fillna方法用法及代碼示例
- Python Pandas DataFrame empty屬性用法及代碼示例
- Python Pandas DataFrame pop方法用法及代碼示例
- Python Pandas DataFrame nsmallest方法用法及代碼示例
- Python Pandas DataFrame sample方法用法及代碼示例
- Python Pandas DataFrame items方法用法及代碼示例
- Python Pandas DataFrame max方法用法及代碼示例
- Python Pandas DataFrame swaplevel方法用法及代碼示例
- Python Pandas DataFrame agg方法用法及代碼示例
- Python Pandas DataFrame copy方法用法及代碼示例
- Python Pandas DataFrame pow方法用法及代碼示例
- Python Pandas DataFrame insert方法用法及代碼示例
- Python Pandas DataFrame lt方法用法及代碼示例
- Python Pandas DataFrame all方法用法及代碼示例
- Python Pandas DataFrame unstack方法用法及代碼示例
- Python Pandas DataFrame mean方法用法及代碼示例
- Python Pandas DataFrame tz_convert方法用法及代碼示例
- Python Pandas DataFrame isin方法用法及代碼示例
- Python PySpark DataFrame collect方法用法及代碼示例
注:本文由純淨天空篩選整理自Isshin Inada大神的英文原創作品 Pandas DataFrame | floordiv method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。
