Pandas 设置为数据帧的第一行

Pandas 设置为数据帧的第一行,pandas,Pandas,我想在某个列中为数据帧的前n行设置一个值 >>> example = pd.DataFrame({'number':range(10),'name':list('aaabbbcccc')},index=range(20,0,-2)) # nontrivial index >>> example name number 20 a 0 18 a 1 16 a 2 14 b 3 12

我想在某个列中为数据帧的前n行设置一个值

>>> example = pd.DataFrame({'number':range(10),'name':list('aaabbbcccc')},index=range(20,0,-2)) # nontrivial index
>>> example
   name  number
20    a       0
18    a       1
16    a       2
14    b       3
12    b       4
10    b       5
8     c       6
6     c       7
4     c       8
2     c       9
我想将第一行的“number”设置为数字19,比如5行。我真正想要的是将“number”的最小值设置为该值,所以我只是先排序。 如果我的索引是微不足道的,我可以

example.loc[:5-1,'number'] = 19 # -1 for inclusive indexing
# or 
example.ix[:5-1,'number'] = 19
但由于不是这样,这将产生以下工件(其中选择了最多4个索引值):

使用.iloc[]会更好,只是它不接受列名

example.iloc[:5]['number'] = 19
可以工作,但提供了一个设置,带有CopyWarning

我目前的解决方案是:

>>> example.sort_values('number',inplace=True)
>>> example.reset_index(drop=True,inplace=True)
>>> example.ix[:5-1,'number'] = 19
>>> example
  name  number
0    a      19
1    a      19
2    a      19
3    b      19
4    b      19
5    b       5
6    c       6
7    c       7
8    c       8
9    c       9
由于我必须对几列重复这一操作,我必须重复几次,每次都重置索引,这也会消耗我的索引(但没关系)


有谁有更好的解决方案吗?

如果重复某些索引,我会将.iloc用作.loc,可能会产生意外的结果

example.loc[example.index[:5], 'number'] = 19
example.iloc[:5, example.columns.get_loc('number')] = 19

这仅在索引不重复时有效。example=pd.DataFrame({'number':range(10),'name':list('aaabbbccc')},index=range(10,0,-2)*2)example.loc[example.index[:5],'number']=19将无法按预期工作,它将设置为重复索引。@ErnestScribbler您尝试过吗?首先,您没有指定索引是非唯一的。这通常是您希望在示例中包括的内容,如果它是您关心的内容。其次,我运行了一个具有重复索引的示例,结果很好。第三,如果您坚持
example.loc[example.index[:5].unique(),'number']=19
这是一个很好的解决方案,可以与其他过滤配对。非常感谢。
example.iloc[:5, example.columns.get_loc('number')] = 19