1

I have this pandas dataframe:

area    name    colA    colB
2.7     val 3.760826923 3.502604167
0.12    val 5.96003125  5.833534
12.9    val 3.595288462 3.243333
21.81   val 5.037025    4.571475667
57.42   val 2.132785714 2.456866667
0.03    val 3.0935  3.229389833
2.07    val 3.336634615 3.421142

Is there a way to convert all numeric columns into cumulative sums, so that resulting dataframe is like this:

area    name    colA    colB
2.7     val 3.760826923 3.502604167
2.82    val 9.720858173 9.336138167
15.72   val 13.31614663 12.57947117
37.53   val 18.35317163 17.15094683
94.95   val 20.48595735 19.6078135
94.98   val 23.57945735 22.83720333
97.05   val 26.91609196 26.25834533

NOTE: Please note that I would strongly prefer not manually specifying column names

user308827
  • 21,227
  • 87
  • 254
  • 417

2 Answers2

3

Yes. Just use the cumsum method

df[['area', 'colA', 'colB]] = df[['area', 'colA', 'colB]].cumsum()

If you want to get all numerics this answer is good and the below is my spin on it.

# create list of numpy numeric types that you want
numerics = [np.int16, np.int32, np.int64, np.float16, np.float32, np.float64]

# make it a numpy dtype object
numeric_dtype = [np.dtype(dt) for dt in numerics]

# get the dtypes of every column - this is a Series
col_types = df.dtypes

# get just the numeric columns
numeric_cols = col_types[col_types.isin(numeric_dtype)].index

# do all the numeric columns
df[numeric_cols] = df[numeric_cols].cumsum()
Community
  • 1
  • 1
Ted Petrou
  • 59,042
  • 19
  • 131
  • 136
1

Method 1: Explicitly selecting numeric dtypes

df[df.select_dtypes(include=['float64']).columns.tolist()] = \
                                     df.select_dtypes(include=['float64']).cumsum()
df

Method 2: Calling "_get_numeric_data" method on DF

df[df._get_numeric_data().columns.tolist()]=df._get_numeric_data().cumsum()
df

Output:

enter image description here

Mohammad Yusuf
  • 16,554
  • 10
  • 50
  • 78