Skip to content
Advertisement

python pandas how to compute on rows with same index values

I have a dataframe called resulttable that looks like:

JavaScript

where df Index values are the index values when resulttable is printed or exported to xls, Tag = str, and Exp. m/z, Intensity, and Norm_Intensity are float64. The tag values will be coming from the file names in a specified folder, so they can vary.

As you can see, each tag contains similar Exp. m/z values (like 1013.328, 1013.325, 1013.329) with their corresponding Intensity & Norm_Intensity values. These m/z values are to be considered the same, and I was wondering if there is a way to do calculations such as mean, std, CV on the row values based on their df index values. It doesn’t matter if another dataframe needs to be made for the calculations.

JavaScript

doesn’t seem to do what I wanted. Doing so results in:

JavaScript

EDIT The output of print(resulttable.index) is:

JavaScript

I assume the labels correspond to each tag.

Advertisement

Answer

Your index is actually a MultiIndex. Since you’re passing this to groupby() you’re going to produce a grouping on every combination of the MultiIndex. I’m assuming you’re looking to group by df Index– if so you’ll need to group on level=0.

Try this:

JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement