简体   繁体   中英

How do I assign a value to a pandas dataframe cell with a missing value with the value of another cell based on a condition?

I have a dataframe that has some missing values. I want to replace those missing values with a value from another cell in the dataframe based on a condition. So the dataframe looks like this:

x a
xyz A
lmn B
None A
xyz A
qrs C
None B

What I want to do is set the value of the "None" cell to the value in column x when the values in column a match. So that it looks like this:

x a
xyz A
lmn B
xyz A
xyz A
qrs C
lmn B

The index is just sequential numbers from 0 up and may change depending on the dataset so the index for the cells with the missing information will change.

You can use ffill() to fill forward missing values:

df['x'] = df.replace('None', np.nan).groupby('a')['x'].ffill()
print(df)

# Output:
     x  a
0  xyz  A
1  lmn  B
2  xyz  A
3  xyz  A
4  qrs  C
5  lmn  B
for i in range(len(df)):
    if df['a'][i] == 'A':
        df['x'][i] = 'xyz'

This worked for me, if you want to do all the other letters, just add an elif .

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