Skip to content

Tag: pandas

applymap() does not work on Pandas MultiIndex Slice

I have an hierarchical dataset: I want to apply a function to all values under the columns A. I can set the value to something: Easy enough. Now, instead of assigning a value, if I want to apply a mapping to this MultiIndex slice, it does not work. For example, let me apply a simple formatting statement: This…

pandas DataFrame style, highlight nan’s

Say with this DataFrame How can I check which element is nan inside df.applymap? (ie, not using df.isnull) The problem comes from where I want to use the pandas html styling. We have the built-in nan highlighting but it changes the background colour, instead I want “nan” to be displayed in red. So…

Use .corr to get the correlation between two columns

I have the following pandas dataframe Top15: I create a column that estimates the number of citable documents per person: I want to know the correlation between the number of citable documents per capita and the energy supply per capita. So I use the .corr() method (Pearson’s correlation): I want to ret…

replace part of the string in pandas data frame

I have pandas data frame in which I need to replace one part of the vale with another value for Example. I have out of which I’d like to replace ony the HF – part thus the result would be I have tried pd.replace() but it doesnt work as I need only one part of the string replaced, rather than

pandas convert columns to percentages of the totals

I have a dataframe with 4 columns an ID and three categories that results fell into I would like to convert it to percentages ie: this seems like it should be within pandas capabilities but I just can’t figure it out. Thanks in advance! Answer You can do this using basic pandas operators .div and .sum, …