Pandas different mathematical operation, conditional on column value

Advertisements data= {‘start_value’:[10,20,30,40,50,60,70], ‘identifier’:[‘+’,’+’,’-‘,’-‘,’+’,’-‘,’-‘]} df = pd.DataFrame(data) start_value identifier 0 10 + 1 20 + 2 30 – 3 40 – 4 50 + 5 60 – 6 70 – I am attempting to created a new column "end_value" that results in either +5 or -5 to the *"*start_value" column based on the "+" or… Read More Pandas different mathematical operation, conditional on column value

Error on creating conditional column in R

Advertisements Thank you for your kind responses. I have a dataframe and I need to apply a formula over a columns depending of the value of other column: data <- data.frame(figure = c("square","square","circle","square","circle"), diameter =c(NA,NA,21,NA,12), side=c(32,27,NA,51,NA)) What I need is to calculate the area according the square or circle formula (square = side * side,… Read More Error on creating conditional column in R

Calculating row sums in data frame based on column names

Advertisements I have a data frame with media spending for different media channels: TV <- c(200,500,700,1000) Display <- c(30,33,47,55) Social <- c(20,21,22,23) Facebook <- c(30,31,32,33) Print <- c(50,51,52,53) Newspaper <- c(60,61,62,63) df_media <- data.frame(TV,Display,Social,Facebook, Print, Newspaper) My goal is to calculate the row sums of specific columns based on their name. For example: Per definition… Read More Calculating row sums in data frame based on column names

Django count and store values in models

Advertisements I have multiple Models which look like this class Classes(models.Model): User = models.ForeignKey(User, on_delete=models.SET_NULL, null=True) A1 = models.IntegerField(default=0) B1 = models.IntegerField(default=0) etc A2 = models.IntegerField(default=0) B2 = models.IntegerField(default=0) etc A3 = models.IntegerField(default=0) B3 = models.IntegerField(default=0) etc A4 = models.IntegerField(default=0) B4 = models.IntegerField(default=0) etc Sum_of_1 = models.IntegerField( blank=True, null=True) Sum_of__2 = models.IntegerField( blank=True, null=True) Sum_of__3… Read More Django count and store values in models

Creating A New Calculated Category Within A Column in R

Advertisements Suppose I have a data frame similar to this, only with 1000’s of observations: df <- data.frame(Group = c(‘A’, ‘A’, ‘A’, ‘B’, ‘B’, ‘B’,’B’,’C’,’C’,’C’,’D’,’D’,’D’,’D’,’D’), Values=c(‘5′,’7′,’9′,’0′,’8′,’4′,’5′,’2′,’1′,’3′,’6′,’3′,’1′,’3′,’5’)) What I want to do is add a new calculated group to the data frame based on values in a group that already exists in the data frame without… Read More Creating A New Calculated Category Within A Column in R

How to calculate the persondays in a year based on two date columns

Advertisements I would like to count the amount of days an ID/row(person) has during a period of 1 year. Below an example of my dataset. (Inschrijfdatum = registration date, Uitschrijfdatum = deregistration date) Inschrijfdatum Uitschrijfdatum <date> <date> 1 1996-02-22 2019-01-11 2 2011-10-31 2019-02-25 3 1992-06-15 2019-03-10 4 2001-11-13 2022-01-01 5 2019-02-18 2019-09-07 6 2019-12-30 2022-01-01… Read More How to calculate the persondays in a year based on two date columns

How to create and populate pandas columns based on cell values

Advertisements I have created a dataframe called df as follows: import pandas as pd d = {‘feature1’: [1, 22,45,78,78], ‘feature2’: [33, 2,2,65,65], ‘feature3’: [100, 2,359,87,2],} df = pd.DataFrame(data=d) print(df) The dataframe looks like this: I want to create two new columns called Freq_1 and Freq_2 that count, for each record, how many times the number… Read More How to create and populate pandas columns based on cell values