Pandas conditional creation of multi columns

pandas create new column based on multiple condition
pandas replace values in column based on multiple condition
pandas if condition
pandas create new column based on condition
create pandas column with new values based on values in other columns
creating conditional column pandas
pandas condition on two columns
pandas if else multiple columns

Say I have a dataframe like this:

x y z class
1 2 3   0
2 2 3   0
1 4 5   2
3 2 2   1

I would like to assign each class a different color value (RGB). So I need to insert three columns right after column z based on the class:

x y z  r   g   b class
1 2 3 255 254 253  0
2 2 3 255 254 253  0
1 4 5  0  255  0   2
3 2 2  0   0  255  1

Currently I am doing it like this:

# insert three columns
df['r']=0
df['g']=0
df['b']=0
# replace r/g/b values based on `class`
def colorit(dataframe):
    colors = [[255, 254, 253], [0, 0, 255], [0, 255, 0]]
    for i in range(3):
        dataframe.loc[dataframe['c']==i, 'r'] = colors[i][0]
        dataframe.loc[dataframe['c']==i, 'g'] = colors[i][1]
        dataframe.loc[dataframe['c']==i, 'b'] = colors[i][2]

But I think there should be some way to make use of the apply or map method or something like that to have this done more elegantly and efficiently (with less code and no loops).

join

Create a dataframe from the colors list and join on the 'class' column.

colors = [[255, 254, 253], [0, 0, 255], [0, 255, 0]]

c = pd.DataFrame(colors, columns=[*'rgb'])
df.join(c, on='class')

   x  y  z  class    r    g    b
0  1  2  3      0  255  254  253
1  2  2  3      0  255  254  253
2  1  4  5      2    0  255    0
3  3  2  2      1    0    0  255

assign

With some array slicing

df.assign(**dict(zip('rgb', np.array(colors)[df['class']].T)))

   x  y  z  class    r    g    b
0  1  2  3      0  255  254  253
1  2  2  3      0  255  254  253
2  1  4  5      2    0  255    0
3  3  2  2      1    0    0  255

Pandas conditional creation of a series/dataframe column, If you only have two choices to select from: df['color'] = np.where(df['Set']=='Z', '​green', 'red'). For example, import pandas as pd import numpy as np df = pd. Using Pandas to create a conditional column by selecting multiple columns in two different dataframes

You could do

In [237]: df.assign(**pd.DataFrame([colors[x] for x in df['class']], columns=['r', 'g', 'b']))
Out[237]:
   x  y  z  class    r    g    b
0  1  2  3      0  255  254  253
1  2  2  3      0  255  254  253
2  1  4  5      2    0  255    0
3  3  2  2      1    0    0  255

Details

In [238]: df
Out[238]:
   x  y  z  class
0  1  2  3      0
1  2  2  3      0
2  1  4  5      2
3  3  2  2      1

In [239]: colors
Out[239]: [[255, 254, 253], [0, 0, 255], [0, 255, 0]]

Using Pandas to create a conditional column by selecting multiple , I was recently doing some data transformations and faced a situation where I had to select multiple columns from two different dataframes and  Suppose you have an online store. The price of the products is updated frequently. While calculating the final price on the product, you check if the updated price is available or not. If not available then you use the last price available. Solution #1: We can use conditional expression to check if the column is present or not.

Using merge

df.merge(pd.DataFrame(data=colors,columns=list('rgb')).rename_axis('class').reset_index())
Out[468]: 
   x  y  z  class    r    g    b
0  1  2  3      0  255  254  253
1  2  2  3      0  255  254  253
2  1  4  5      2    0  255    0
3  3  2  2      1    0    0  255

5 ways to apply an IF condition in pandas DataFrame, Suppose that you created a DataFrame in Python that has 10 numbers (from 1 to 10). df.loc[df['column name'] condition, 'new column name'] = 'value if condition is met' There are indeed multiple ways to apply such a condition in Python. I would like to create a new column with a numerical value based on the following conditions: a. if gender is male & pet1=pet2, points = 5 b. if gender is female & (pet1 is 'cat' or pet1='dog'), points = 5 c. all other combinations, points = 0.

How To Create a Column Using Condition on , Next we will use Pandas' apply function to do the same. Let us first load How to Create a Column Using A Condition in Pandas using NumPy? Let us use the How to Create Pandas Dataframe from Multiple Lists? Pandas  Python Pandas : Select Rows in DataFrame by conditions on multiple columns. In this article we will discuss different ways to select rows in DataFrame based on condition on single or multiple columns. Select Rows based on value in column. It will return a DataFrame in which Column ‘Product‘ contains ‘Apples‘ only i.e.

Conditional operation on Pandas DataFrame columns , How to drop one or multiple columns in Pandas Dataframe · Using dictionary to remap values in Pandas DataFrame columns · Change Data Type for one or more  Create a Column Based on a Conditional in pandas. 20 Dec 2017. Preliminaries # Import required modules import pandas as pd import numpy as np. Make a dataframe.

Python, While operating on data, there could be instances where we would like to add a column based on some condition. There does not exist any library function to  I'd like to create a new column based on the used column, so that the df looks like this: portion used alert 0 1 1.0 Full 1 2 0.3 Partial 2 3 0.0 Empty 3 4 0.8 Partial Create a new alert column based on; If used is 1.0, alert should be Full. If used is 0.0, alert should be Empty.