12

I have a text file which has been read into pandas by df1 = pandas.read_csv(r'fruits.txt', sep=',')

    item  freshness
0   apple    2.2
1   pear     0.0

and a series of calculations that would yield the result of apple = 2.3

Is it possible to do a pandas.update so that I can update the value of freshness for apple in the dataframe to 2.3?

1 Answer 1

22

IIUC you need loc:

apple = 2.3

print df['item'] == 'apple'
0     True
1    False
Name: item, dtype: bool

df.loc[df['item'] == 'apple', 'freshness'] = apple
print df
    item  freshness
0  apple        2.3
1   pear        0.0
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.