Pandas how to create a new column by seleting correct values from origianl column?

Question:

How to create a new column(C) by dynamic select the value from column(B)?
For example as blow, if column B’s value bigger than 3 than put than into new column(C)

Original DataFrame

  A B
0 a 2
1 b 1
2 c 4
3 d 9

Expect output

  A B C
0 a 2 4/9
1 b 1 4/9
2 c 4 4/9
3 d 9 4/9 

Kindly remind that: each rows of column(C) are the same. 4 & 9 are selected and splited them with ‘/’ (Should be string of course)

Asked By: smelling lady

||

Answers:

If need join by / all values of B column greater like 3 by Series.gt use:

df['C'] = '/'.join(df.loc[df.B.gt(3), 'B'].astype(str))
print (df)
   A  B    C
0  a  2  4/9
1  b  1  4/9
2  c  4  4/9
3  d  9  4/9
Answered By: jezrael
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.