0

continuing from my last question

Supposed that I expanded my dataframe to something that looks like this

import pandas as pd
import numpy as np

na = np.nan

df = pd.DataFrame({
    'location' : ['a','a','a','a','a','b','b','b','b','b'],
    'temp' : [11.6,12.2,na,12.4,12.9,na,17.6,17.4,17.2,16.8],
    'precip': [0.22,0.22,0.24,na,0.27,0.33,na,0.34,0.34,0.37]
})

And I only want to fill out missing temp values on location a. I tried something like this

df[df['location'] == 'a']['temp'] = df[df['location'] == 'a']['temp'].interpolate()

And it giving me this error

<string>:12: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
location  temp  precip
0        a  11.6    0.22
1        a  12.2    0.22
2        a   NaN    0.24
3        a  12.4     NaN
4        a  12.9    0.27
5        b   NaN    0.33
6        b  17.6     NaN
7        b  17.4    0.34
8        b  17.2    0.34
9        b  16.8    0.37

Any help or reference would be appreciated. Thank you

2 Answers 2

1

Use DataFrame.loc:

m = df['location'] == 'a'
df.loc[m, 'temp'] = df.loc[m, 'temp'].interpolate()

Your solution (slowier if large df, because 2 times created mask):

df.loc[df['location'] == 'a', 'temp'] = df.loc[df['location'] == 'a', 'temp'].interpolate()
Sign up to request clarification or add additional context in comments.

Comments

0

You can use update to modify values in place.

df.update(df.loc[df['location'] == 'a', 'temp'].interpolate())
print(df)

# Output:
  location  temp  precip
0        a  11.6    0.22
1        a  12.2    0.22
2        a  12.3    0.24
3        a  12.4     NaN
4        a  12.9    0.27
5        b   NaN    0.33
6        b  17.6     NaN
7        b  17.4    0.34
8        b  17.2    0.34
9        b  16.8    0.37

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.