# Generate square symmetric matrix in R

I have a matrix in R as follows: dat <- matrix(c(2,3,5,7,8,4), ncol = 6) colnames(dat) <- c("A(1,1)", "A(1,2)", "A(1,3)", "A(2,2)", "A(2,3)", "A(3,3)") How can I create a square symmetric matrix based on an apply function that has the following form: A(1,1) A(1,2) A(1,3) A(2,1) A(2,2) A(2,3) A(3,1) A(3,2) A(3,3) Note that A(1,2)=A(2,1) >Solution : This… Read More Generate square symmetric matrix in R

# KeyError(key) in get_loc_level after using .transform() or apply()

I have a large grouped data frame with multiple groups where I’m trying to filter rows within each group. To simplify it, I will share a simplified data frame with one group where I’m getting the error. df5 is grouped by "Detail", "ID", "Year" data2 = {"Year":["2012","2012","2012","2012","2012","2012","2012","2012","2012"], "Country":[‘USA’,’USA’,’USA’,’USA’,’USA’,’USA’,’USA’,’CANADA’,"CANADA"], "Country_2": ["", "", "", "", "", "",… Read More KeyError(key) in get_loc_level after using .transform() or apply()

# Match each dictionary value with csv column entry and apply dictionary key to new column

I want to match dictionary values with an entry in a csv dataframe column and put the corresponding dictionary key in a new column. I have a csv dataframe with a Link and six columns with numbers. I have a dictionary with Sites and many Links. import pandas as pd # reproducible data data =… Read More Match each dictionary value with csv column entry and apply dictionary key to new column

# pairwise subtraction of columns in a dataframe in R

I was wondering is there a way to automate (e.g., loop) the subtraction of (X2-X1), (X3-X1), (X3-X2) in my data below and add them as three new columns to the data? m=" id X1 X2 X3 A 1 0 4 B 2 2 2 C 3 4 1" data <- read.table(text = m, h =… Read More pairwise subtraction of columns in a dataframe in R

# r – How to fit a lm() function on a list of data sets?

After simulating 100,000 observations from DGP and splitting them to create a list of 1000 data frames with 100 observations each I would like to fit the the same equation to each data frame separately. I was wondering how to get separate coefficients for each data frame? α <- 6 ß_1 <- 0.5 ß_2 <-… Read More r – How to fit a lm() function on a list of data sets?

# I am getting this error: TypeError: '<' not supported between instances of 'str' and 'float'

I have this table that in which I am comparing list of articles (Article_body) with 4 baseline articles using cosine similarity: Article_body articleScores1 articleScores2 articleScores3 articleScores4 articleScores5 a***** 0.6 0.2 0.7 0.9 0.2 a***** 0.3 0.8 0.1 0.5 0.1 I want to add a column that gives which column has the largest cosine similarity out… Read More I am getting this error: TypeError: '<' not supported between instances of 'str' and 'float'

# Detecting Keys in a Column of Strings

I have a dictionary with key and value pairs. I also have a data frame with a column containing strings that contain the various keys. If a key appears in the column in the data frame, I’d like to append the corresponding value in the adjacent column my_dict = {‘elon’ : ‘is awesome’, ‘jeff’ :… Read More Detecting Keys in a Column of Strings

# Find shapes of dataframes inside lambda functions

I have the following dataframe with pairs of strings in tuples d = {‘value’: [[‘Red’, ‘Blue’], [‘Blue’, ‘Yellow’], [‘Blue’, ‘Yellow’], [‘Yellow’, ‘Orange’], [‘Green’, ‘Purple’], [‘Purple’, ‘Yellow’], [‘Yellow’, ‘Red’], [‘Violet’, ‘Blue’], [‘Blue’, ‘Green’], [‘Green’, ‘Red’], [‘Red’, ‘Brown’], [‘Blue’, ‘Green’]]} df = pd.DataFrame(data = d) And I want to find for each row probability, which can be… Read More Find shapes of dataframes inside lambda functions

# value_counts not working in groupby apply

I am using .apply(pd.Series.value_counts, axis=0) to count the values in two pandas columns [‘a’,’b’]. However when I try and use it after grouping on column ‘Group’, I get the error: TypeError: value_counts() got an unexpected keyword argument ‘axis’ It works when grouping in a for loop, but not with a groupby apply. Here is code… Read More value_counts not working in groupby apply