Python 设置数据帧中列的第一行和最后一行

Python 设置数据帧中列的第一行和最后一行,python,pandas,Python,Pandas,我一直在读这篇文章,但仍然发现这个主题有点令人困惑: 假设我有一个Pandas DataFrame,我希望同时将单个列的第一行和最后一行元素设置为任意值。我可以这样做: df.iloc[[0,-1]].mycl=[1,2] 这告诉我,一个值正试图在一个数据帧的切片副本上设置。,这有潜在的危险 我可以改为使用.loc,但我需要知道第一行和最后一行的索引(相反,.iloc允许我按位置访问) 做这件事最安全的方法是什么 要达到这一点: # Django queryset query = market

我一直在读这篇文章,但仍然发现这个主题有点令人困惑:

假设我有一个Pandas DataFrame,我希望同时将单个列的第一行和最后一行元素设置为任意值。我可以这样做:

df.iloc[[0,-1]].mycl=[1,2]

这告诉我,
一个值正试图在一个数据帧的切片副本上设置。
,这有潜在的危险

我可以改为使用
.loc
,但我需要知道第一行和最后一行的索引(相反,
.iloc
允许我按位置访问)

做这件事最安全的方法是什么

要达到这一点:

# Django queryset
query = market.stats_set.annotate(distance=F("end_date") - query_date)

# Generate a dataframe from this queryset, and order by distance
df = pd.DataFrame.from_records(query.values("distance", *fields), coerce_float=True)
df = df.sort_values("distance").reset_index(drop=True)

然后,我尝试调用
df.distance.iloc[[0,-1]]=[1,2]
。这会引发警告。

您正在执行的操作称为链式索引,您可以仅在该列上使用
iloc
来避免警告:

In [24]:
df = pd.DataFrame(np.random.randn(5,3), columns=list('abc'))

Out[24]:
          a         b         c
0  1.589940  0.735713 -1.158907
1  0.485653  0.044611  0.070907
2  1.123221 -0.862393 -0.807051
3  0.338653 -0.734169 -0.070471
4  0.344794  1.095861 -1.300339

In [25]:
df['a'].iloc[[0,-1]] ='foo'
df

Out[25]:
          a         b         c
0       foo  0.735713 -1.158907
1  0.485653  0.044611  0.070907
2   1.12322 -0.862393 -0.807051
3  0.338653 -0.734169 -0.070471
4       foo  1.095861 -1.300339
In [27]:
df.iloc[[0,-1]]['a'] ='foo'

C:\WinPython-64bit-3.4.3.1\python-3.4.3.amd64\lib\site-packages\IPython\kernel\__main__.py:1: 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
  if __name__ == '__main__':
如果您以另一种方式操作,则会发出警告:

In [24]:
df = pd.DataFrame(np.random.randn(5,3), columns=list('abc'))

Out[24]:
          a         b         c
0  1.589940  0.735713 -1.158907
1  0.485653  0.044611  0.070907
2  1.123221 -0.862393 -0.807051
3  0.338653 -0.734169 -0.070471
4  0.344794  1.095861 -1.300339

In [25]:
df['a'].iloc[[0,-1]] ='foo'
df

Out[25]:
          a         b         c
0       foo  0.735713 -1.158907
1  0.485653  0.044611  0.070907
2   1.12322 -0.862393 -0.807051
3  0.338653 -0.734169 -0.070471
4       foo  1.095861 -1.300339
In [27]:
df.iloc[[0,-1]]['a'] ='foo'

C:\WinPython-64bit-3.4.3.1\python-3.4.3.amd64\lib\site-packages\IPython\kernel\__main__.py:1: 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
  if __name__ == '__main__':

问题不在于
iloc
,而是在访问
.mycl
时创建了一个副本。您可以在
iloc
中完成这一切:

df.iloc[[0, -1], df.columns.get_loc('mycol')] = [1, 2]

如果您想要混合整数和基于标签的访问,通常使用
ix
,但在这种情况下不起作用,因为
-1
实际上不在索引中,而且显然
ix
不够聪明,不知道它应该是最后一个索引。

确实
df['mycl'].iloc[[0,-1]=[1,2]
起作用?同样的警告,无论我是先索引行还是先索引列。您需要准确地显示导致警告的所有步骤,因为我的回答表明这可以正确地编辑我的原始帖子。谢谢。实际上,
df.mycol.iloc[[0,-1]]
仍然会给我一个警告。我是0.17.1。我想知道这是否是因为它是使用
pd.DataFrame.from_records
创建的,因为当我运行你的时,没有警告。我在0.18.0上也有同样的问题。当我运行上面的代码时,我没有得到警告,但是当我在我创建的一些测试数据上使用相同的方法时,发出了警告。奇怪。有时会让您希望Python使指针的概念更加明确;)啊!!这就是创建副本的地方。谢谢