【问题标题】:Why I am getting prompt when manipulating df created from another df为什么我在操作从另一个 df 创建的 df 时得到提示
【发布时间】:2021-12-14 23:04:34
【问题描述】:

我有以下代码:

#converts SQLite data to a Panda data frame
df1 = pd.read_sql_query("SELECT * FROM Accounts", conn)
df2 = pd.read_sql_query("SELECT * FROM Rates", conn)
df3 = pd.read_sql_query("SELECT * FROM Clients", conn)

#Create new data frame as working space
dfnew= df1[['Client','Product','CCY','Balance']]

#map Currency code
dfnew['fxrate'] = dfnew.CCY.map(df2.set_index('Currency code')['Rate'])

dfnew.head()

当我运行结果时,输出符合预期。但是我收到以下消息:

C:\ProgramData\Anaconda3\lib\site-packages\ipykernel_launcher.py:14: 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: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy

这是什么意思? 我的代码有问题吗?

【问题讨论】:

    标签: python sql pandas


    【解决方案1】:

    dfnew 不是新的数据框,而是 df1 的视图

    你应该复制一份:

    #Create new data frame as working space
    dfnew = df1[['Client','Product','CCY','Balance']].copy()
    

    注意。您应该阅读警告消息中的链接,这会详细解释问题

    【讨论】:

      猜你喜欢
      • 2021-11-07
      • 2021-09-28
      • 1970-01-01
      • 2020-11-12
      • 2023-03-07
      • 2020-01-06
      • 1970-01-01
      • 2022-12-03
      • 2019-09-04
      相关资源
      最近更新 更多