我有这个pandas数据帧:
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
有没有办法将所有数字列转换为累积总和,以便生成的数据框如下所示:
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
注意: 请注意,我强烈不希望不手动指定列名
答案 0 :(得分:3)
是。只需使用cumsum
方法
df[['area', 'colA', 'colB]] = df[['area', 'colA', 'colB]].cumsum()
如果你想获得所有数字this answer is good,下面是我的旋转。
# 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()
答案 1 :(得分:1)
方法1:明确选择数字dtypes
Int main()
Int N= 2;
Int board(Int array[N][N]);
{
Int i = 1;
Int j = 10;
while(i<10)
{
printf(i);
i++;
while(j>0)
printf(j);
j--;
}
}
方法2:在DF上调用“_get_numeric_data”方法
df[df.select_dtypes(include=['float64']).columns.tolist()] = \
df.select_dtypes(include=['float64']).cumsum()
df
输出: