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

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'

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