I have a dataframe like below and I want to add another column that is replicated untill certain condition is met.
JavaScript
x
10
10
1
sample_df = pd.DataFrame(data={
2
'id': ['A', 'B', 'C'],
3
'n' : [ 1, 2, 3],
4
'v' : [ 10, 13, 8],
5
'z' : [5, 3, 6],
6
'g' : [8, 8, 10]
7
})
8
9
additional_rows=
10
Now I want to add another column which contains additional information about the dataframe. For instance, I want to replicate Yes
untill id
is B
and No when it is below B
and Yes
from C
to D
and from from D
to E
Maybe
.
The output I am expecting is as follows:
JavaScript
1
18
18
1
sample_df = pd.DataFrame(data={
2
'id': ['A', 'B', 'C','G','D','E'],
3
'n' : [ 1, 2, 3, 5, 5, 9],
4
'v' : [ 10, 13, 8, 8, 4 , 3],
5
'z' : [5, 3, 6, 9, 9, 8],
6
'New Info': ['Yes','Yes','No','No','Maybe','Maybe']
7
})
8
9
sample_df
10
11
id n v z New Info
12
0 A 1 10 5 Yes
13
1 B 2 13 3 Yes
14
2 C 3 8 6 No
15
3 G 5 8 9 No
16
4 D 5 4 9 Maybe
17
5 E 9 3 8 Maybe
18
How can I achieve this in python?
Advertisement
Answer
You can use np.select
to return results based on conditions. Since you were talking more about positional conditions I used df.index
:
JavaScript
1
18
18
1
sample_df = pd.DataFrame(data={
2
'id': ['A', 'B', 'C','G','D','E'],
3
'n' : [ 1, 2, 3, 5, 5, 9],
4
'v' : [ 10, 13, 8, 8, 4 , 3],
5
'z' : [5, 3, 6, 9, 9, 8]
6
})
7
8
sample_df['New Info'] = np.select([sample_df.index<2, sample_df.index<4],['Yes', 'No'], 'Maybe')
9
sample_df
10
Out[1]:
11
id n v z New Info
12
0 A 1 10 5 Yes
13
1 B 2 13 3 Yes
14
2 C 3 8 6 No
15
3 G 5 8 9 No
16
4 D 5 4 9 Maybe
17
5 E 9 3 8 Maybe
18