如何将空列添加到数据框? - 跟进
How to add an empty column to a dataframe? - followup
已回答此问题 here。然而,具有以下输入:
print(type(df1))
df1['x'] = np.nan
我得到以下输出:
<class 'pandas.core.frame.DataFrame'>
C:\ProgramData\Anaconda3\lib\site-packages\ipykernel_launcher.py:2: 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
我的案例与附件中的案例有何不同 link?
此警告的真正原因可能是在您创建 df1
时回退了几行代码。如果你有这样的事情:
df1 = another_df[['col1', 'col2']]
有时您会得到原始数据框的一部分,有时您会得到一个副本。说出你得到了什么并不容易。我相信它与原始框架中的值与引用语义有关。
要消除警告,请进行显式复制:
df1 = another_df[['col1', 'col2']].copy()
df1['new_col'] = np.na
已回答此问题 here。然而,具有以下输入:
print(type(df1))
df1['x'] = np.nan
我得到以下输出:
<class 'pandas.core.frame.DataFrame'>
C:\ProgramData\Anaconda3\lib\site-packages\ipykernel_launcher.py:2: 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
我的案例与附件中的案例有何不同 link?
此警告的真正原因可能是在您创建 df1
时回退了几行代码。如果你有这样的事情:
df1 = another_df[['col1', 'col2']]
有时您会得到原始数据框的一部分,有时您会得到一个副本。说出你得到了什么并不容易。我相信它与原始框架中的值与引用语义有关。
要消除警告,请进行显式复制:
df1 = another_df[['col1', 'col2']].copy()
df1['new_col'] = np.na