dataframe

How to check if a value in one dataframe is present in keys in the other dataframe

随声附和 提交于 2021-02-13 17:40:28
问题 I have two dataframes: df_1: Letters Boolean a Nan b Nan c Nan df_2: a b d 2.7 1.2 3.6 1 2 3 How do I check if df_1['letters'] is present in df_2.keys(). If it is present, I want boolean to take the value 'x': Something like: Letters Boolean a x b x c Nan I tried using this code: for x in df_1['letters']: if x in df_2.keys(): df_1['Boolean']='x' 回答1: Use numpy.where with isin: df1['Boolean'] = np.where(df1['Letters'].isin(df2.columns), 'x', np.nan) 回答2: You need : df1['Boolean']=df1.Letters

How to check if a value in one dataframe is present in keys in the other dataframe

主宰稳场 提交于 2021-02-13 17:38:08
问题 I have two dataframes: df_1: Letters Boolean a Nan b Nan c Nan df_2: a b d 2.7 1.2 3.6 1 2 3 How do I check if df_1['letters'] is present in df_2.keys(). If it is present, I want boolean to take the value 'x': Something like: Letters Boolean a x b x c Nan I tried using this code: for x in df_1['letters']: if x in df_2.keys(): df_1['Boolean']='x' 回答1: Use numpy.where with isin: df1['Boolean'] = np.where(df1['Letters'].isin(df2.columns), 'x', np.nan) 回答2: You need : df1['Boolean']=df1.Letters

Add new value to new column based on if value exists in other dataframe in R

主宰稳场 提交于 2021-02-13 05:38:47
问题 I have two dataframes called users and purchases with thousands of datasets to each. Both have a feature called ID . My aim is to add a new column called buyer to the dataframe purchases , if the value of ID of purchases exists in ID of users . So the two dataframes look like this: users = data.frame("ID" = c(23432,75645,5465645,5656,6456)) purchases = data.frame("ID" = c(6456,4436,88945)) It should look like: 回答1: We can use %in% to compare the values and wrap as.integer to convert logical

Merge values of a dataframe where other columns match

旧街凉风 提交于 2021-02-11 18:24:48
问题 I have a dataframe storing a date, car_brand, color and a city: date car_brand color city "2020-01-01" porsche red paris "2020-01-02" prosche red paris "2020-01-03" porsche red london "2020-01-04" porsche red paris "2020-01-05" porsche red london "2020-01-01" audi blue munich "2020-01-02" audi red munich "2020-01-03" audi red london "2020-01-04" audi red london "2020-01-05" audi red london I now want to create from that a dataframe in the following way: Merge rows together where for

adding data input into a empty data frame in R using shiny

梦想与她 提交于 2021-02-11 18:21:47
问题 I am fairly new to shiny and R I am trying to make a user interface for the amount of money used in different companies. I made a empty data frame, and was trying to add the input from the user to the data frame, but the app crashes. Any assistance would be great please! library(shiny) df <- data.frame(Company = character(), Goods = character(),GoodsType = character(), Number = integer(), Cost = double(), stringsAsFactors = FALSE) ui <- fluidPage( titlePanel("Cost tool "), sidebarLayout(

adding data input into a empty data frame in R using shiny

别等时光非礼了梦想. 提交于 2021-02-11 18:21:07
问题 I am fairly new to shiny and R I am trying to make a user interface for the amount of money used in different companies. I made a empty data frame, and was trying to add the input from the user to the data frame, but the app crashes. Any assistance would be great please! library(shiny) df <- data.frame(Company = character(), Goods = character(),GoodsType = character(), Number = integer(), Cost = double(), stringsAsFactors = FALSE) ui <- fluidPage( titlePanel("Cost tool "), sidebarLayout(

How can I keep the rows of a pandas data frame that match a particular condition using value_counts() on multiple columns

折月煮酒 提交于 2021-02-11 18:01:53
问题 I would like to get rid of those rows where a particular value occurs only once in a column, considering 3 columns. That is, for feature: text: if value_counts() == 1, then eliminate those rows, or just keep when value_counts() > 1 next_word: if value_counts() == 1, then eliminate those rows, or just keep when value_counts() > 1. In this case, just work with the already processed (just kept the rows that the column 'text' contains values showing up more than once) previous_word: if value

How can I keep the rows of a pandas data frame that match a particular condition using value_counts() on multiple columns

蹲街弑〆低调 提交于 2021-02-11 18:01:05
问题 I would like to get rid of those rows where a particular value occurs only once in a column, considering 3 columns. That is, for feature: text: if value_counts() == 1, then eliminate those rows, or just keep when value_counts() > 1 next_word: if value_counts() == 1, then eliminate those rows, or just keep when value_counts() > 1. In this case, just work with the already processed (just kept the rows that the column 'text' contains values showing up more than once) previous_word: if value

Pandas - Rolling average for a group across multiple columns; large dataframe

三世轮回 提交于 2021-02-11 17:51:48
问题 I have the following dataframe: -----+-----+-------------+-------------+-------------------------+ | ID1 | ID2 | Box1_weight | Box2_weight | Average Prev Weight ID1 | +-----+-----+-------------+-------------+-------------------------+ | 19 | 677 | 3 | 2 | - | +-----+-----+-------------+-------------+-------------------------+ | 677 | 19 | 1 | 0 | 2 | +-----+-----+-------------+-------------+-------------------------+ | 19 | 677 | 3 | 1 | (0 + 3 )/2=1.5 | +-----+-----+-------------+-----------

Pandas - Rolling average for a group across multiple columns; large dataframe

喜你入骨 提交于 2021-02-11 17:51:37
问题 I have the following dataframe: -----+-----+-------------+-------------+-------------------------+ | ID1 | ID2 | Box1_weight | Box2_weight | Average Prev Weight ID1 | +-----+-----+-------------+-------------+-------------------------+ | 19 | 677 | 3 | 2 | - | +-----+-----+-------------+-------------+-------------------------+ | 677 | 19 | 1 | 0 | 2 | +-----+-----+-------------+-------------+-------------------------+ | 19 | 677 | 3 | 1 | (0 + 3 )/2=1.5 | +-----+-----+-------------+-----------