简体   繁体   中英

pandas how to fill NaN/None values based on the other columns?

Given the following, how can I set the NaN/None value of the B row based on the other rows? Should I use apply?

d = [
    {'A': 2, 'B': Decimal('628.00'), 'C': 1, 'D': 'blue'},
    {'A': 1, 'B': None, 'C': 3, 'D': 'orange'},
    {'A': 3, 'B': None, 'C': 1, 'D': 'orange'},
    {'A': 2, 'B': Decimal('575.00'), 'C': 2, 'D': 'blue'},
    {'A': 4, 'B': None, 'C': 1, 'D': 'blue'},
]

df = pd.DataFrame(d)

# Make sure types are correct
df['B'] = df['B'].astype('float')
df['C'] = df['C'].astype('int')

In : df
Out:
   A    B  C       D
0  2  628  1    blue
1  1  NaN  3  orange
2  3  NaN  1  orange
3  2  575  2    blue
4  4  NaN  1    blue

In : df.dtypes
Out:
A      int64
B    float64
C      int64
D     object
dtype: object

Here is an example of the "rules" to set B when the value is None:

def make_B(c, d):
    """When B is None, the value of B depends on C and D."""
    if d == 'blue':
        return Decimal('1400.89') * 1 * c
    elif d == 'orange':
        return Decimal('2300.57') * 2 * c
    raise

Here is the way I solve it:

I define make_B as below:

def make_B(x):
    if np.isnan(x['B']):
        """When B is None, the value of B depends on C and D."""
        if x['D'] == 'blue':
            return Decimal('1400.89') * 1 * x['C']
        elif x['D'] == 'orange':
            return Decimal('2300.57') * 2 * x['C']
    else:
        return x['B']

Then I use apply:

df.apply(make_B,axis=1)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM