pandas 从数据框的最后一行中选择特定列的正确方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/46141866/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Correct way to select a specific column from the last row of a Dataframe
提问by user2663139
I have a dataframe stock_pick
and trying to set last row of certain column like
我有一个数据框stock_pick
并试图设置某些列的最后一行,例如
stock_pick.iloc[-1]["Regime"] = 0
This results in the ,
这导致 ,
/home/prowler/analysis-toolkit/anaconda2/envs/py3.6/lib/python3.6/site-packages/pandas/core/indexing.py:179: SettingWithCopyWarning:
A value is trying to be set on a copy of a slice from a DataFrame
See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
self._setitem_with_indexer(indexer, value)
What is the correct way to pick and assign a value to a specific column in the last row?
为最后一行中的特定列选择和分配值的正确方法是什么?
回答by jezrael
You can use get_loc
for position of column and thn is posssible use DataFrame.iloc
:
您可以get_loc
用于列的位置,然后可以使用DataFrame.iloc
:
stock_pick.iloc[-1, stock_pick.columns.get_loc("Regime")] = 0
Another solution is select by DataFrame.loc
and select index by position by [-1]
:
另一种解决方案是 select byDataFrame.loc
和 select index by position by [-1]
:
stock_pick.loc[stock_pick.index[-1], "Regime"] = 0