0
a question on SO with the title "Set value for particular cell in pandas DataFrame"がありますが、行インデックスがわかっているものとします。関連する列の行の値が一意である場合、値を変更するにはどうすればよいですか?最も簡単なオプションの下にset_indexを使用していますか? index.tolistでインデックス値のリストを返すことは、非常に洗練されたソリューションのようではありません。ここでインデックスが不明で列の値が一意である場合、パンダのデータフレームに単一の値を置き換えます。
は私のコードです:ここでは
import pandas as pd
## Fill the data frame.
df = pd.DataFrame([[1, 2], [3, 4]], columns=('a', 'b'))
print(df, end='\n\n')
## Just use the index, if we know it.
index = 0
df.set_value(index, 'a', 5)
print('set_value', df, sep='\n', end='\n\n')
## Define a new index column.
df.set_index('a', inplace=True)
print('set_index', df, sep='\n', end='\n\n')
## Use the index values of column A, which are known to us.
index = 3
df.set_value(index, 'b', 6)
print('set_value', df, sep='\n', end='\n\n')
## Reset the index.
df.reset_index(inplace = True)
print('reset_index', df, sep='\n')
は私の出力です:
a b
0 1 2
1 3 4
set_value
a b
0 5 2
1 3 4
set_index
b
a
5 2
3 4
set_value
b
a
5 2
3 6
reset_index
a b
0 5 2
1 3 6
ありがとう!私はちょうど今、他の質問スレッドで同じ答えを見つけました。ちょうど投票がほとんどありませんでした。 https://stackoverflow.com/a/38467449/778533 –
@ tommy.carstensen質問は最初に尋ねられてから約4年後の非常に最近の回答です。 :) – Psidom