Merging dataframes on one column while replacing values with another column

Advertisements I have two dataframes: mapping = pd.DataFrame({‘idx’: [‘a’,’b’,’c’], ‘val’: [1, 2, 3]}) obs = pd.DataFrame({‘obs’: [‘a’,’c’,’a’,’a’,’b’,’d’]}) I would like for all observations in obs, that are present in mappings idx column, to get the value of mappings val column. If the value does not exist in mappings idx column, it should be discarded. That… Read More Merging dataframes on one column while replacing values with another column

Finding which cell a point belongs to given that the cells are not of the same size

Advertisements I have a map that is divided into unequally sized cells. I want to find out which cell a point belongs to. For a grid where the cells are equally shaped, this is fairly simple. However, how do I solve the problem where the cells are unequal? Specifically: given a pandas dataframe of cells… Read More Finding which cell a point belongs to given that the cells are not of the same size

how do you convert data frame column values to integer

Advertisements I need to convert data frame column to int. df[‘Slot’].unique() displays this: array([‘1’, ‘2’, ‘3’, ‘4’, 1, 3, 5], dtype=object) some values have ” around it some dont. I tried to convert the data type to int as below: df[‘Slot’]=df[‘Slot’].astype(‘Int64’) I get this error: TypeError: cannot safely cast non-equivalent object to int64 Any ideas… Read More how do you convert data frame column values to integer

Pandas Column containing a list of matched strings found using str.contains('|'.join(words))

Advertisements I have string data that I am matching to a list of terms and I want to create a new column that shows all words found in each row of the dataframe df = pd.DataFrame({‘String’: [‘Cat Dog Fish’, ‘Cat Dog’, ‘Pig Horse’, ‘DogFish’]}) print(df) String 0 Cat Dog Fish 1 Cat Dog 2 Pig… Read More Pandas Column containing a list of matched strings found using str.contains('|'.join(words))

Split the entries in dataframe

Advertisements here is my sample data: df=pd.DataFrame({‘Name’:[‘A,B’,’C’,’D’,’E,F,G’] ,’Age’:[4,6,8,9]}) My expected output is to split the entries if there are more than one names. pd.DataFrame({‘Name’:[‘A’,’B’,’C’,’D’,’E’,’F’,’G’] ,’Age’:[4,4,6,8,9,9,9]}) I can only split the name but I don’t now how to make it duplicated entries. For first row, there are A,B under the Name, so I want to make… Read More Split the entries in dataframe

Dictionary from data frame. Values assigned to tuple keys derived from column, row products

Advertisements Given a data frame structured as follows: df = pd.DataFrame({ ‘DATE’ : [1,2,3,4,5], ‘Q24’ : [23.28, 28.81, 29.32, 29.8, 30.25], ‘J24’ : [24.22, 24.89, 25.54, 26.15, 26.73], ‘F24’ : [22.34, 32.73, 33.1, 33.45, 33.77] }) I would like to create a dictionary in which all keys are tuples containing the products of values in… Read More Dictionary from data frame. Values assigned to tuple keys derived from column, row products