我有一个如下所示的 df,(前 2 行是文本,第一列是日期)
In [4]: df
Out[4]:
test bs dv if ir md qb sy tb
0 TESTacc a10900 a10900 a10900 IJJMKK11 a10900 a10900 a10900 a10900
1 01-Feb-2019 18.8668013 4.6021207 0.9330807 13.9766832 2.9002571 0.2824343 0.8280988 0.8587644
2 04-Feb-2019 16.187526 3.1000162 0.4145835 14.6465183 2.848472 0.2516608 0.8618771 0.218063
Run Code Online (Sandbox Code Playgroud)
我需要得到这个具有 3 个小数精度的 csv 另外我需要添加一个“总计”列(最右边的列)我已经尝试了以下内容,但这些都不正确
要添加我所做的总列:
ndf=df.iloc[2:,1:] #take only numerics in ndf
ndf = ndf.apply(pd.to_numeric)
ndf=ndf.round(3)
df['total']=ndf.sum(axis=1)
Run Code Online (Sandbox Code Playgroud)
这不是做简单的事情的正确方法,比如添加一个总列
所以我尝试过,
df=df.apply(pd.to_numeric,errors='ignore')
但 round 仍然无法在 df 上工作我的意图是只添加一个 Total 列并将所有数字四舍五入到小数点后 3 位。附加:完成此操作后,我将添加最后一行作为中位数行,每列都有中位数
根据最新的 Pandas 文档1.0.3,您只能使用以下代码对数字列求和:
df_sum = df.sum(numeric_only = True)
Run Code Online (Sandbox Code Playgroud)
这将对中的所有数字列求和df
并将其分配给变量df_sum
。
IIUC,您可能需要:
df['sum']=df.apply(lambda x: pd.to_numeric(x,errors='coerce')).sum(axis=1).round(3)
#for median: df.apply(lambda x: pd.to_numeric(x,errors='coerce')).median(axis=1).round(3)
print(df)
test bs dv if ir md \
0 TESTacc a10900 a10900 a10900 IJJMKK11 a10900
1 01-Feb-2019 18.8668013 4.6021207 0.9330807 13.9766832 2.9002571
2 04-Feb-2019 16.187526 3.1000162 0.4145835 14.6465183 2.848472
qb sy tb sum
0 a10900 a10900 a10900 0.000
1 0.2824343 0.8280988 0.8587644 43.248
2 0.2516608 0.8618771 0.218063 38.529
Run Code Online (Sandbox Code Playgroud)
您可以使用编辑df.where()
,将所有数字舍入为:
df['sum']=df.apply(lambda x: pd.to_numeric(x,errors='coerce')).sum(axis=1)
df=(df.where(df.apply(lambda x: pd.to_numeric(x,errors='coerce')).isna(),
df.apply(lambda x: pd.to_numeric(x,errors='coerce')).round(3)))
print(df)
test bs dv if ir md qb sy \
0 TESTacc a10900 a10900 a10900 IJJMKK11 a10900 a10900 a10900
1 01-Feb-2019 18.867 4.602 0.933 13.977 2.9 0.282 0.828
2 04-Feb-2019 16.188 3.1 0.415 14.647 2.848 0.252 0.862
tb sum
0 a10900 0.000
1 0.859 86.496
2 0.218 77.057
Run Code Online (Sandbox Code Playgroud)