[英]How do I make bins of equal number of observations in a pandas dataframe?
[英]How to make part of two pandas dataframe equal
我正在嘗試根據 4 個條件將相同大小(1000000 行和 8 列)的兩個數據幀(static_df_1 和 static_df_2)的一部分設置為彼此相等。 但是,我無法使它們相等。 i 和 j 是每個數據框中的兩列,sales 也是這兩個數據框中的另一個共享列。 我的條件是僅將兩個數據幀中 25 < i < 36 和 25 < j < 36 的部分設置為相等。當我執行以下代碼時,它們仍然不同且不相等!
old_sales = static_df_1.loc[(static_df_1['i'] > 25 ) & (static_df_1['i'] < 36) & (static_df_1['j'] > 25 ) & (static_df_1['j'] < 36 )]['sales']
static_df_2.loc[(static_df_2['i'] > 25 ) & (static_df_2['i'] < 36) & (static_df_2['j'] > 25 ) & (static_df_2['j'] < 36 )]['sales'] = old_sales
通常你會用
df.loc[row_indexer,column_indexer]
也許將事情分開以使其更易於評估。
row_indexer = (static_df_1['i'] > 25 ) & (static_df_1['i'] < 36) & (static_df_1['j'] > 25 ) & (static_df_1['j'] < 36 )
old = static_df_1.loc[row_indexer,'sales']
static_df_2.loc[row_indexer,'porosity'] = old
我沒有在這里安裝 Pandas,所以我無法測試。
來自 Pandas 用戶指南中的布爾索引(重點是我的):
通過標簽選擇、位置選擇和高級索引選擇方法,您可以使用布爾向量與其他索引表達式組合沿多個軸進行選擇。
也來自不同的索引選擇:
.loc 主要基於標簽,但也可以與布爾數組一起使用
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.