Quantcast
Channel: Recent Questions - Stack Overflow
Viewing all articles
Browse latest Browse all 22544

Add a sub-column to multi-level dataframe

$
0
0

My dataframe looks like

    First    Second    A   B    A   B 1   50  60   70  1002   20  5    10  303   35  45   60  50

I want to add a new sub-column in each top-level column using .diff(), so it would look like (in this case, taking the differences of the B column)

       First          Second    A   B   Diff     A    B    Diff1   50  60  NaN      70   100  NaN2   20  5   -55      10   30   -703   35  45  40       60   50   20 

I think that there should be a clean way to do this without for loops and other inefficient native python operations but I'm not sure how.


Viewing all articles
Browse latest Browse all 22544

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>