python pandas 数据帧使 r(t-1).iloc[1:] = r(t).iloc[0:-1]
python pandas dataframe to make r(t-1).iloc[1:] = r(t).iloc[0:-1]
我想让pandas的数据框的一列,第二行到最后一行等于另一列的第一行到倒数第二行:
它看起来像:
r(t-1).iloc[1:] = r(t).iloc[0:-1](r(t) and r(t-1))
是同一数据框的列
我遇到的问题是 python 无法理解我想要移动复制数据行的想法:
IR['r(t-1)'].iloc[1:] = IR['r(t)'].iloc[0:-1]
/anaconda3/lib/python3.6/site-packages/pandas/core/indexing.py:190:
SettingWithCopyWarning:
试图在 DataFrame
的切片副本上设置一个值
查看文档中的注意事项:http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
self._setitem_with_indexer(indexer, value)
有人知道怎么处理吗?
欢迎来到 SO。
如何解决你的问题
您的其中一个专栏似乎需要 shift。正如您问题的前两行所示。您可以通过 pandas 数据帧的 shift()
方法来完成。那么你的答案可能是:
df['new_row'] = df['old_row'].shift(1)
您可以使用正负移位值向前或向后移位
你运行遇到的错误是什么
简而言之,pandas提供了数据帧的写入和读取方式,实际上不接受任何其他方式。该警告表明您没有使用写入数据框(使用 iloc
)的好方法(pandas 拙见!),因为 iloc
主要用于按索引访问行.
请各位pandas高手指正,如有错误请指正
我想让pandas的数据框的一列,第二行到最后一行等于另一列的第一行到倒数第二行: 它看起来像:
r(t-1).iloc[1:] = r(t).iloc[0:-1](r(t) and r(t-1))
是同一数据框的列
我遇到的问题是 python 无法理解我想要移动复制数据行的想法:
IR['r(t-1)'].iloc[1:] = IR['r(t)'].iloc[0:-1]
/anaconda3/lib/python3.6/site-packages/pandas/core/indexing.py:190:
SettingWithCopyWarning: 试图在 DataFrame
的切片副本上设置一个值查看文档中的注意事项:http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
self._setitem_with_indexer(indexer, value)
有人知道怎么处理吗?
欢迎来到 SO。
如何解决你的问题
您的其中一个专栏似乎需要 shift。正如您问题的前两行所示。您可以通过 pandas 数据帧的 shift()
方法来完成。那么你的答案可能是:
df['new_row'] = df['old_row'].shift(1)
您可以使用正负移位值向前或向后移位
你运行遇到的错误是什么
简而言之,pandas提供了数据帧的写入和读取方式,实际上不接受任何其他方式。该警告表明您没有使用写入数据框(使用 iloc
)的好方法(pandas 拙见!),因为 iloc
主要用于按索引访问行.
请各位pandas高手指正,如有错误请指正