# 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

# Calculate sum column filtering identical values on multiple columns

Advertisements I have data for multiple columns (S1,S2,S3) and I’m trying to create a sum column (result). I want to sum values for each row that has identical values in S1, S2 and S3 columns. Here is a sample data and the result I’m looking for. S1 <- c(1,1,1,0,1,0) S2 <- c(1,1,1,0,1,0) S3 <- c(1,0,0,0,0,0)… Read More Calculate sum column filtering identical values on multiple columns

# 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 new columns with NA in starting columns

Advertisements I have data.frame(col1 = 1:4, col2 = c(NA,1,2,3), col3 = 5:8) and want to create extra columns which is based on computations involving the other columns with NA in it, but keep getting NA as a result. I do not want to see NA, but want to see 1, based on df %>% mutate(… Read More calculating new columns with NA in starting columns

# summing a column based on values in two other columns

Advertisements I have a data frame that lists individual mass shootings for each state between 1991-2020. I would like to 1) sum the total victims each year for each state, and 2) sum the total number of mass shootings each state had each year. So far, I’ve only managed to get a total sum of… Read More summing a column based on values in two other columns

# 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