Posts

Showing posts with the label dataframe

Increase from previous value by condition in R [duplicate]

Increase from previous value by condition in R [duplicate] This question already has an answer here: I was searching for an answer to my specific problem, but I didn't find a conclusion. I have a dataframe with data ID a 1 0 2 0 3 1 4 1 5 1 6 1 7 0 8 1 9 1 10 0 11 1 12 0 13 0 Now i want to add "b" column with number increase from previous b if a == 1 Result like this ID a b 1 0 0 2 0 0 3 1 1 4 1 2 5 1 3 6 1 4 7 0 0 8 1 1 9 1 2 10 0 0 11 1 1 12 0 0 13 0 0 14 1 1 15 1 2 16 1 3 17 1 4 Thanks in advance! This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question. 3 Answers 3 Using dplyr an option can be to group on cumsum(a==0) . This will create a group which got a previous row (if available with a=0 for all rows with a=...

python count /sum records accross row with multiple condition [on hold]

python count /sum records accross row with multiple condition [on hold] I current have a table which A,B,C represents product values while AA,BB,CC represents product type A B C AA BB CC 10 20 0 1 2 0 5 0 15 1 2 3 i also have 2 arrays which are: product =['A','B','C'] Product Value = ['AA','BB','CC'] My question is 1) To count/sum corresponding product type if its product value >0 product value >0 Product value Product Type(PT) count/sum same product if its value >0 ------------- ---------------- ---------------------------------------------------- A B C AA BB CC product_1 product_2 product_1_sum product_2_sum 10 20 0 1 1 0 2 0 30 0 5 0 15 0 2 2 0 2 0 15 for first record, product A and B have value > 0 and A and B are...

Searching a Python Dictionary with multiple values

Image
Searching a Python Dictionary with multiple values I have data in the following csv file, available here: http://s000.tinyupload.com/index.php?file_id=87473936848618674050 Screenshot of the CSV: I've written the following code to import the CSV file into Python as a Pandas Dataframe, and then the code after that creates a Dict. The dictionary has to have name and region as the keys, and the Windows and Linux prices as the dictionary values. #Import libraries and CSV file into dataframe, renaming columns, printing head import pandas as pd df = pd.read_csv('file.csv') col_names = ['Name','Region','API', 'Memory','vCPU', 'Storage', 'Linux', 'Windows' ] df.columns = col_names #Creating Dict dict = {} for i in df.index: key = (df.at[i, 'Name'] , df.at[i, 'Region']) #Rename columns accordingly value = (df.at[i, 'vCPU'], df.at[i, 'Memory'], df.at[i, 'Storage'], df.at[i, ...

How to flatten (or explode) the data along with row in a Dataframe, based on column data?

How to flatten (or explode) the data along with row in a Dataframe, based on column data? Data frame should explode based on SPC column. Below is example My Input DataFrame. ID Name Level SPC Rating salry 23 sam 3 HBS 3.5 4000 43 Nair 4 KSTk 4 5000 56 Rom 5 MNC 3 3000 My output should be: ID Name level SPC Rating Salary 23 sam 3 H 3.5 4000 23 sam 3 B 3.5 4000 23 sam 3 S 3.5 4000 43 Nair 4 K 4 5000 43 Nair 4 S 4 5000 43 Nair 4 T 4 5000 43 Nair 4 k 4 5000 How can I resolve this problem in Scala or Java code? 2 Answers 2 If you have a dataframe/dataset as +---+----+-----+----+------+------+ |ID |Name|Level|SPC |Rating|salary| +---+----+---...

Merge two columns into one, element by element in R

Merge two columns into one, element by element in R I have the following data: df <- data.frame(estimate = c(1,2), sd = c("(0.5)*","(0.5)***")) rownames(df) <- c("race","gender") I want to make a journal quality table by merging the two columns into one, element by element. The desired output would be: Results race 1 (0.5)* gender 2 (0.5)*** try m1 <- matrix(c(t(df)));row.names(m1) <- c(rbind(row.names(df), "")) – akrun Jul 1 at 1:23 m1 <- matrix(c(t(df)));row.names(m1) <- c(rbind(row.names(df), "")) Awesome! Thanks for such a beautiful solution. – Leonhardt Guass Jul 1 at 1:25 ...

How to extract the first 2 digits of all numbers in a column of a dataframe?

How to extract the first 2 digits of all numbers in a column of a dataframe? I am completely new at Python (this is my first assignment) and I am trying to take the first two digits of the D-column of the following dataframe and put those two digits in a new column F: import pandas as pd import numpy as np df1 = pd.DataFrame({'A' : [1, 1, 1, 4, 5, 3, 3, 4, 1, 4], 'B' : [8, 4, 3, 1, 1, 6, 4, 6, 9, 8], 'C' : [69,82,8,25,56,79,98,68,49,82], 'D' : [1663, 8818, 9232, 9643, 4900, 8568, 4975, 8938, 7513, 1515], 'E' : ['Married','Single','Single','Divorced','Widow(er)','Single','Married','Divorced','Married','Widow(er)']}) I found several possible solutions here on Stack Overflow, and tried to apply them but none of them is working for me. Either I get some error message (different depending on wh...