Frenze
Frenze

Reputation: 31

Creating a new column from the values of a column - Pandas

I want to create a new column on pandas related to the info that I have on column C and want to create a column D..The data that I have has 50k columns so it is impossible for me to do it manually..

A sample of the data is ;

        A           B              C
        12          12            3:02
        13          13            2:02
        14          14            3:03
        15          15            1:04
        16          16            2:05

I need to dive the values into 2 parts at column C from the colon symbol ;
if the first value is bigger than the second like in row 1 == 3>02 the value on D column value will be A
if both values are equal like in rows 2 and 3 (2:02/3:03) the value on D column value will be B
if second value is bigger than the first value like in rows 4 and 5 (1:04 /2:05 ) D column value will be C

so the new data will look like

    A            B             C           D
    2           12            3:02         A  
    13          13            2:02         B   
    14          14            3:03         B  
    15          15            1:04         C  
    16          16            2:05         C

Thanks in advance .

Upvotes: 1

Views: 61

Answers (1)

jezrael
jezrael

Reputation: 862471

Use numpy.select with new DataFrame created by Series.str.split and expand=True:

df1 = df['C'].str.split(':', expand=True).astype(int)
print(df1)
   0  1
1  3  2
2  2  2
3  3  3
4  1  4
5  2  5

df['D'] = np.select([df1[0] > df1[1], df1[0] == df1[1], df1[0] < df1[1]], ['A','B','C'])
print (df)
    A   B     C  D
1  12  12  3:02  A
2  13  13  2:02  B
3  14  14  3:03  B
4  15  15  1:04  C
5  16  16  2:05  C

Upvotes: 1

Related Questions