我正在研究熊貓資料框。第一件事是根據我在和中指定的數字(df1)從原始資料幀中過濾并創建一個新資料幀,然后下一步是將 更新為另一個數字,并通過從 更新來完成。(df)num_posts columnuser column is user1num_postsdfdf1
原來的df是:
df = pd.DataFrame({'num_posts': [4, 4, 3, 4, 1, 14],
'date': ['2020-08-09', '2020-08-25',
'2020-09-05', '2020-09-12',
'2020-09-29', '2020-10-15'],
'user': ['user1', 'user1', 'user2', 'user3', 'user4', 'user4']})
# The new filtered df1
# filter posts that equal 4 and user is user1
df1 = df.loc[(df['num_posts'] == 4) & (df['user'] == 'user1')]
df1
# overwrite the num_posts column with 10
for i in df1.index:
df1.loc[i, 'num_posts'] = 10
# Updating the original dataframe df with df1
df.update(df1)
df
當我運行我的代碼時,會顯示以下警告。
C:\Program Files\Python38\lib\site-packages\pandas\core\indexing.py:1817: SettingWithCopyWarning:
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead
See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
self._setitem_single_column(loc, value, pi)
在打開警告訊息中的鏈接時,我被重定向到 pandas 官方網站,問題似乎是鏈式索引。我需要幫助才能知道如何擺脫它并在連續過濾相同的原始資料幀 df 時避免它。
uj5u.com熱心網友回復:
如果有幫助,試試這個:
#df1 = df.loc[(df['num_posts'] == 4)].copy()
df1 = df.loc[(df['num_posts'] == 4) & (df['user'] == 'user1')].copy()
描述在這里
輸出
num_posts date user
0 10.0 2020-08-09 user1
1 10.0 2020-08-25 user1
2 3.0 2020-09-05 user2
3 4.0 2020-09-12 user3
4 1.0 2020-09-29 user4
5 14.0 2020-10-15 user4
uj5u.com熱心網友回復:
你可以像這樣解決它。
這是故意使用問題意圖的“提取部分,修改,寫回”策略撰寫的。
df = pd.DataFrame({'num_posts': [4, 4, 3, 4, 1, 14],
'date': ['2020-08-09', '2020-08-25',
'2020-09-05', '2020-09-12',
'2020-09-29', '2020-10-15'],
'user': ['user1', 'user1', 'user2', 'user3', 'user4', 'user4']})
# The new filtered df1
# filter posts that equal 4 and user is user1
df1 = df.loc[(df['num_posts'] == 4) & (df['user'] == 'user1')].copy()
df1
# overwrite the num_posts column with 10 in the filtered part
df1.loc[:, 'num_posts'] = 10
# Updating the original dataframe df with df1
df.loc[df1.index] = df1
df
但是,您也可以像這樣處理過濾后的部分:
post_filter = (df['num_posts'] == 4) & (df['user'] == 'user1')
# overwrite num_posts directly in the filtered part
df.loc[post_filter, 'num_posts'] = 10
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/489754.html
