I have Dataframe like:
Now I would like to add column V based on conditions on I1, I2 and I3. Conditions are like:
v = 1 if I1>23 and I2.str.contains('abc')
v = 2 if I3 == 20
v == ...............
...................
One row can satisfy multiple condition what I want is to multiply such rows and filter out the rows which is not satisfying any condition such as suppose N1 will satisfy for V=1,2 and 3. While N2 does not satisfy any and N3 will satisfy v=2. I want the final dataframe to look like:
Could someone please help me with this? Thanks.
If I understood your question correctly, suppose you have a dataframe like the below:
df = pd.DataFrame({
"NAME": [ "N1", "N2", "N3" ],
"I1": [ 1, 4, 4 ],
"I2": [ 2, 5, 2 ],
"I3": [ 3, 6, 6 ]
})
ie:
>>> df
NAME I1 I2 I3
0 N1 1 2 3
1 N2 4 5 6
2 N3 4 2 6
To reproduce your example, I will assume that the conditions are I1 = 1
, I2 = 2
, and I3 = 3
:
cond1 = df["I1"] == 1
cond2 = df["I2"] == 2
cond3 = df["I3"] == 3
To build the expected dataframe, you can do:
result = pd.concat([
df[cond1].assign(V=1),
df[cond2].assign(V=2),
df[cond3].assign(V=3)
])
Result:
>>> result
NAME I1 I2 I3 V
0 N1 1 2 3 1
0 N1 1 2 3 2
2 N3 4 2 6 2
0 N1 1 2 3 3
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.