【问题标题】:Python Pandas Plot WarningPython Pandas 绘图警告
【发布时间】:2020-02-14 04:27:40
【问题描述】:

当我在 spyder 中使用 pandas 绘图数据时。它总是会显示一条警告消息:

C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\indexing.py:494: SettingWithCopyWarning: 试图在 DataFrame 中的切片副本上设置一个值。 尝试使用 .loc[row_indexer,col_indexer] = value 代替。 请参阅文档中的注意事项:http://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy self.obj[项目] = s

我的脚本如下:

import pandas as pd

loc0 = r'D:\DC_BP00010.T0058_20190804_082758_15_14_PM2_1031.txt'
loc1 = r'D:\DC_BP00010.T0058_20190804_082758_17_16_PM1_1193.txt'

locs = [loc0,loc1]  # Update files in the list

parameters_must_be = ['Time', 'StepNo (Int)'] # Do not change this one
parameters = ["HeadBEPTrend1 (Float)"] # Key in parameters needed
parameters_all = parameters_must_be.append(parameters) 

endpoint_steps = [3] # Update endpoint steps in the list

Titles = 'BP00010' # Titles for plot

Colors = ['black','red','brown','crimson','olive','blue','yellow','darkorange','lime','purple','deeppink','dodgerblue','orange','indigo','darkslateblue','lawngreen','darkslategray','darkgreen','midnightblue','lightseagreen','gold','maroon','navy','teal']

dfs = []
sa = []
ss = []
axs = []
n = len(locs) # Number of datalog files

for i in range(0,n):
    dfs.append(pd.read_csv(locs[i], sep=('\t'), skiprows=6, usecols=parameters_all))
    ss.append(dfs[i][dfs[i]['StepNo (Int)'].isin(endpoint_steps)])
    sa.append(ss[i].loc[:,'Time']-ss[i].loc[:,'Time'].iloc[0])
    ss[i].loc[:,'Time'] = sa[i].loc[:]

ax=ss[0].plot(x='Time', y=parameters, title=Titles, color=Colors[0:len(parameters)], linewidth=1)

if n>1:
    for i in range(1,n):
        axs.append(ss[i].plot(x='Time', y=parameters, color=Colors[len(parameters)*i:len(parameters)*i+len(parameters)], linewidth=1, ax=ax))

ax.set_xlim(0,80)
ax.set_ylim(0,60)
ax.set_ylabel('Endpoint Intensity')
ax.legend(['BP00010#14','BP00010#15','BP00010#16','BP00010#17'], bbox_to_anchor=(1.31, .5), loc=5, borderaxespad=0.)

【问题讨论】:

  • 您在哪一行出现错误?
  • 其实这就是我想知道的
  • 我猜,这条线会引起警告dfs[i][dfs[i]['StepNo (Int)'].isin(endpoint_steps)]。您的语法在这一行似乎很奇怪。你想在这条线上实现什么?

标签: python pandas plot


【解决方案1】:

ss[i].loc[:,'Time'] = sa[i].loc[:] 导致错误:

  • 您正在尝试更改另一个数据框中的值。
  • ss 是数据框列表
  • ss[i] 是来自 dfs 的特定数据帧

sssa 是从以下创建的部分数据帧的列表:

  • ss.append(dfs[i][dfs[i]['StepNo (Int)'].isin(endpoint_steps)])
    • ss 来自dfs
  • sa.append(ss[i].loc[:,'Time']-ss[i].loc[:,'Time'].iloc[0])
    • sa 来自ss
  • 从其他数据帧创建数据帧很好,但这样做会引用原始数据,而不是副本。

在创建sssa时添加.copy()

  • ss.append(dfs[i][dfs[i]['StepNo (Int)'].isin(endpoint_steps)].copy())
  • sa.append(ss[i].loc[:,'Time']-ss[i].loc[:,'Time'].iloc[0].copy())

【讨论】:

    猜你喜欢
    • 2019-10-05
    • 2021-12-13
    • 2018-10-02
    • 1970-01-01
    • 2018-04-01
    • 2021-10-30
    • 1970-01-01
    • 2021-05-25
    • 2019-02-02
    相关资源
    最近更新 更多