インデックスを使用してpandas Data Frameの特定のセルに値を設定します.
本論文は以下の通りである:Set value for particular cell in pandas DataFrame using index
I've created a Pandas Data Frame Pandas Data Frameを作成しました
そしてこれを手に入れた
Then I want to assign value to particular cell, for example for row 'C' and column 'x'. 次に、行「C」や列「x」など、特定のセルに値を割り当てます.I've expected to get such result:私はこのような結果を期待しています.
with this code:このコードを使用:
but contents of
Any suggestions? 何かアドバイスはありますか?
1階
参照先:https://stackoom.com/question/w4xU/インデックスを使用してpandas-DataFrameの特定のセルに値を設定します.
2階
RukTech's answer ,
Going forward, the recommended method is
Why
modifies this new dataframe only. この新しいデータ・ボックスのみを変更します.
modifies
Warning : It is sometimes difficult to predict if an operation returns a copy or a view. 警告:操作がコピーまたはビューに戻るかどうかを予測するのは難しい場合があります.For this reason the docs recommend avoiding assignments with "chained indexing". したがって、ドキュメントでは、「リンクインデックス」を使用して値を割り当てることは避けることをお勧めします.
So the recommended alternative isですので推奨される代替方法は
which does modify
#3階
The recommended way(according to the maintainers)to set a value is:推奨方法(メンテナンス者による)は:
Using 'chained indexing' (
See:表示: https://stackoverflow.com/a/21287235/1579844 https://stackoverflow.com/a/21287235/1579844 http://pandas.pydata.org/pandas-docs/dev/indexing.html#indexing-view-versus-copy http://pandas.pydata.org/pandas-docs/dev/indexing.html#indexing-view-versus-copy https://github.com/pydata/pandas/pull/6031 https://github.com/pydata/pandas/pull/6031
#4階
Update: The
The fastest way to do this is using set_value . 一番早い方法はset_を使うことですvalue . This method is ~100 times faster than
#5階
#6階
This is the only thing that worked for me! これは私に役に立つ唯一のものです!
Learn more about
I've created a Pandas Data Frame Pandas Data Frameを作成しました
df = DataFrame(index=['A','B','C'], columns=['x','y'])
そしてこれを手に入れた
x y
A NaN NaN
B NaN NaN
C NaN NaN
Then I want to assign value to particular cell, for example for row 'C' and column 'x'. 次に、行「C」や列「x」など、特定のセルに値を割り当てます.I've expected to get such result:私はこのような結果を期待しています.
x y
A NaN NaN
B NaN NaN
C 10 NaN
with this code:このコードを使用:
df.xs('C')['x'] = 10
but contents of
df
haven't changed. しかしdf
内容は変わらなかった.It's again only NaN
s in DataFrame. 再びDataFrameのNaN
のみです.Any suggestions? 何かアドバイスはありますか?
1階
参照先:https://stackoom.com/question/w4xU/インデックスを使用してpandas-DataFrameの特定のセルに値を設定します.
2階
RukTech's answer ,
df.set_value('C', 'x', 10)
, is far and away faster than the options I've suggested below. RukTechの答えdf.set_value('C', 'x', 10)
は、私が以下で提案した選択肢よりずっと速いです.However, it has been slated for deprecation . しかし、淘汰された.Going forward, the recommended method is
.iat/.at
. 将来を展望して、おすすめの方法は.iat/.at
です.Why
df.xs('C')['x']=10
does not work:なぜdf.xs('C')['x']=10
が機能しないのか:df.xs('C')
by default,returns a new dataframe with a copy of the data,so df.xs('C')
デフォルトでは、データコピー付きの新しいデータdf.xs('C')
が返されるため、df.xs('C')['x']=10
modifies this new dataframe only. この新しいデータ・ボックスのみを変更します.
df['x']
returns a view of the df
dataframe,so df['x']
はdf
データフレームのビューを返すので、df['x']['C'] = 10
modifies
df
itself. df
自体を修正します.Warning : It is sometimes difficult to predict if an operation returns a copy or a view. 警告:操作がコピーまたはビューに戻るかどうかを予測するのは難しい場合があります.For this reason the docs recommend avoiding assignments with "chained indexing". したがって、ドキュメントでは、「リンクインデックス」を使用して値を割り当てることは避けることをお勧めします.
So the recommended alternative isですので推奨される代替方法は
df.at['C', 'x'] = 10
which does modify
df
. 確かにdf
を修正します.In [18]: %timeit df.set_value('C', 'x', 10)
100000 loops, best of 3: 2.9 µs per loop
In [20]: %timeit df['x']['C'] = 10
100000 loops, best of 3: 6.31 µs per loop
In [81]: %timeit df.at['C', 'x'] = 10
100000 loops, best of 3: 9.2 µs per loop
#3階
The recommended way(according to the maintainers)to set a value is:推奨方法(メンテナンス者による)は:
df.ix['x','C']=10
Using 'chained indexing' (
df['x']['C']
) may lead to problems. チェーンインデックス(df['x']['C']
)を使用すると、問題が発生する可能性があります.See:表示:
#4階
Update: The
.set_value
method is going to be deprecated . 更新:.set_value
メソッドは推奨されません..iat/.at
are good replacements,unfortunately pandas provides little documentation .iat/.at
は良い代替品で、残念ながらパンダが提供する書類は少ないですThe fastest way to do this is using set_value . 一番早い方法はset_を使うことですvalue . This method is ~100 times faster than
.ix
method. この方法は.ix
方法より100倍速い.For example:例:df.set_value('C', 'x', 10)
#5階
df.loc[row_index,col_indexer] = value
を使用してみます#6階
This is the only thing that worked for me! これは私に役に立つ唯一のものです!
df.loc['C', 'x'] = 10
Learn more about
.loc
here . .loc
の詳細については、ここで説明します.