gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 22:38:22 24 4
gpt4 key购买 nike

我有一个 DataFrame,它有一个空列“pred”,我希望用一些特定值更新它。它们最初位于 numpy 数组中,但我将它们放在一个名为“this”的系列中: 打印(类型(预测))

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'

最佳答案

尝试:

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

关于python - 如何使用新值更新列的特定 DataFrame 切片?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43578781/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com