Add a new column to a dataframe with multiple condition based on list and a dataframe

Julie Hardy

I have two differents object

a list :

list_color <- c("#f87970", "#c59b04", "#5ab70b", "#04c195", "#06b7eb")

a dataframe :

head(data)
  Cal    Cre
1 ca     h1  
2 cb     h2 
3 ca     h3  
4 cd     h4
5 ce     h5
6 cb     h2 
7 ca     h3  
8 cd     h4

I try to create a new column with a color corresponding to each value from "ca1" column

      Cal    Cre    Color
    1 ca     h1    "#f87970"
    2 cb     h2    "#c59b04"
    3 ca     h3    "#f87970"
    4 cd     h4    "#5ab70b"
    5 ce     h5    "#04c195"
    6 cb     h2    "#c59b04"
    7 ca     h3    "#f87970"
    8 cd     h4    "#5ab70b"

I try to create a double loop, but it is not working. My code :

for(k in list_color){
    for(i in data$Ca1){
       data$Color <- ifelse(i , k, "None")
  }
}
Duck

Try this approach without using loops with dataframes to identify unique values and then assign the color. The approach uses match(). Here the code:

#Colors
list_color <- c("#f87970", "#c59b04", "#5ab70b", "#04c195", "#06b7eb")
dfcolors <- data.frame(id=1:length(list_color),list_color,stringsAsFactors = F)
#Unique values
uni <- unique(df$Ca1)
dfca1 <- data.frame(id=1:length(uni),uni,stringsAsFactors = F)
#Now match ca1 and colors
dfcolors$ca1 <- dfca1[match(dfcolors$id,dfca1$id),"uni"]
#Match with df
df$Color <- dfcolors[match(df$Ca1,dfcolors$ca1),"list_color"]

Output:

df
  Ca1 Cre   Color
1  ca  h1 #f87970
2  cb  h2 #c59b04
3  ca  h3 #f87970
4  cd  h4 #5ab70b
5  ce  h5 #04c195

Some data used:

#Data
df <- structure(list(Ca1 = c("ca", "cb", "ca", "cd", "ce"), Cre = c("h1", 
"h2", "h3", "h4", "h5")), row.names = c("1", "2", "3", "4", "5"
), class = "data.frame")

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

add a new dataframe based on multiple condition in pandas

How to add a new column based another column condition in pandas dataframe

Add a new column to a dataframe based on multiple columns from another dataframe

Add column to dataframe based on condition

Add new column to Multi Index DataFrame Based on Condition

how to add levels to a new column in pandas dataframe based on a condition?

Add multiple new columns to dataframe based on condition in R

Python DataFrame add a new columns based on multiple columns condition

Add new column in a dataframe based on a condition on the content of another column in the same dataframe

Pandas DataFrame add new column values based on group by multiple conditions

add a new column in pyspark dataframe based on matching values from a list

Create a new column based on Condition in Spark Dataframe

Dataframe - Create new column based on condition

Add column to pyspark dataframe based on a condition

How to add a column to dataframe based on condition

Create a Pandas Dataframe column based on multiple condition

Pandas - manipulating column based on multiple condition on dataframe

Adding a new column to a dataframe from the values of another dataframe based on a condition

Creating a new column in a DataFrame based on multiple condition using values from another column in Python

Add new column based on subset of dataframe

Adding new column to a DataFrame based on values in a list

Duplicate list values and add in new column to dataframe

How do I add a new column with a repeated value per group based on condition in a Pandas DataFrame?

Add a new (binary) column in a dataframe, based on certain condition, for every group/id

How to add new column to a dataframe and fill its values based on condition in python

Creating New Column based on condition on Other Column in Pandas DataFrame

Iterate on a pandas dataframe column and create a new column based on condition

How to add a new column into a dataframe based on rows of an other dataframe?

How to add a new column in dataframe based on a single character in dataframe names?