如何使用新值更新特定DataFrame列的切片?

3

我有一个数据框(DataFrame),其中有一个名为“pred”的空列,我希望用一些特定的值来更新它。这些值最初在一个numpy数组中,但我将它们放入了一个叫做“this”的系列(Series)中:

print(type(predictions))

print(predictions)
['collection2' 'collection2' 'collection2' 'collection1' 'collection2'
 'collection1']

this = pd.Series(predictions, index=test_indices)

print(type(data))
<class 'pandas.core.frame.DataFrame'>

print(data.shape)
(35, 4)

print(data.iloc[test_indices])
     class         pred                                          text  \
223  collection2   []  Fellow-Citizens of the Senate and House of Rep...   
20   collection1   []  The period for a new election of a citizen to ...   
12   collection1   []  Fellow Citizens of the Senate and of the House...   
13   collection1   []  Whereas combinations to defeat the execution o...   
212  collection2   []  MR. PRESIDENT AND FELLOW-CITIZENS OF NEW-YORK:...   
230  collection2   []  Fellow-Countrymen:\nAt this second appearing t...   

                                                 title  
223                               First Annual Message  
20                                    Farewell Address  
12                    Fifth Annual Message to Congress  
13   Proclamation against Opposition to Execution o...  
212                               Cooper Union Address  
230                           Second Inaugural Address 

print(type(this))
<class 'pandas.core.series.Series'>

print(this.shape)
(6,)

print(this)
0    collection2
1    collection1
2    collection1
3    collection1
4    collection2
5    collection2

我以为我可以这样做:

我以为我可以像这样做:

data.iloc[test_indices, [4]] = this

但这会导致结果是什么。
IndexError: positional indexers are out-of-bounds

或者

data.ix[test_indices, ['pred']] = this
KeyError: '[0] not in index'
2个回答

5

尝试:

data.loc[data.index[test_indices], 'pred'] = this

2
我更喜欢使用.ix而不是.loc。您可以使用:
data.ix[bool_series, 'pred'] = this

在这里,bool_series是一个布尔系列,包含需要更新值的行为True,否则为False。例如:

bool_series = ((data['col1'] > some_number) & (data['col2'] < some_other_number))

但是,在使用data.ix[bool_series, 'pred']之前,请确保您已经有了一个'pred'列。否则,会出现错误。


2
ix 将被弃用。 - piRSquared
1
ix现在已经消失了,很久很久以前就消失了。使用.iloc(按位置查找)和.loc(通过标签查找)进行查找。 - Salim Fadhley

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接