Stephen
Stephen

Reputation: 121

Conditionally fill a value in a dataframe

I have a column in a dataframe that I need to utilize to fill another column. I have four values that could be in this column - E, H, N, and P. Each of those different values would be used to fill a value in the "Size" column.

An example of my data is as follows:

Type    H Data      P Data      Size
 H        1           0.5
 H       1.5           1
 E        -            -
 N        -            -
 P       0.5          0.5
 E        -            -

I'm wanting to fill in each row of the "Size" column.

For "E" types, I would like to fill in "2". For "N" types, I would like to fill in "-"

For "H" and "P" types, I would like to fill in a value taken from the corresponding dataframe ("H Data" or "P Data").

My result would be expected to be as shown below:

Type    H Data      P Data      Size
 H        1           0.5        1
 H       1.5           1        1.5
 E        -            -         2
 N        -            -         -
 P       0.5          0.5       0.5
 E        -            -         2

What is the best way to do this? I currently have a working code in Excel that accomplishes this, but I'm unsure of how to do it in Python. In Excel, I've got nested IF statements. Is it best to create a loop in Python or are there built in functions into Pandas or NumPy that can accomplish this? Thanks in advance!

Upvotes: 1

Views: 92

Answers (1)

U13-Forward
U13-Forward

Reputation: 71610

Try using:

df['Size'] = df['H Data'].fillna(df['P Data'])
df.loc[df['Type'].eq('E'), 'Size'] = 2
print(df)

Output:

  Type H Data P Data Size
0    H      1    0.5    1
1    H    1.5      1  1.5
2    E      -      -    2
3    N      -      -    -
4    P    0.5    0.5  0.5
5    E      -      -    2

Upvotes: 4

Related Questions