English 中文(简体)
如何在表尾增加一栏总数?
原标题:How to add total column to end of table?
  • 时间:2024-05-12 02:34:53
  •  标签:
  • python
  • sum

我比平时更新,从一位同事继承了一部法典,我似乎无法操作。 设想是,在数据框架中增加一个总行和一栏,但该栏的行目并不奏效,并造成多种错误(见许多“背后(最近的最后一次通话)”):

<代码>csmposting = 产生_csm_table(csm) csmposting [ Total] = csmposting.sum(axis=1) ->这一行文造成错误的贴出错。

当我没有这条线时,我会发现(我去除一栏头):

Unit1   779 285 16  2   3
Unit2   290 20  3   3   0
Unit3   0   0   3   7   0
Unit4   547 97  8   10  0
TOTAL   1616    402 30  22  3

I ve tried changing that line to: csmposting.loc[ TOTAL1 ] = csmposting.sum(axis=1) This only ends up adding it as another row at the bottom of the table and adds a decimal place to all the numbers:

Unit1   779.0   285.0   16.0    2.0 3.0
Unit2   290.0   20.0    3.0 3.0 0.0
Unit3   0.0 0.0 3.0 7.0 0.0
Unit4   547.0   97.0    8.0 10.0    0.0
TOTAL1  NaN NaN NaN NaN NaN
TOTAL   1616.0  402.0   30.0    22.0    3.0

是否有任何人建议如何使整栏显示和保持编号格式不变?

谢谢!

问题回答

您复制该守则的方式,

csmposting = create_csm_table(csm) csmposting[ TOTAL ] = 
csmposting.sum(axis=1) ---->This line causes the error  
csmposting.loc[ TOTAL ] = csmposting.sum(axis=0).astype(int)

似乎有两条指挥部。

页: 1

csmposting = create_csm_table(csm)
csmposting[ TOTAL ] = csmposting.sum(axis=1)  # This line causes the error  
csmposting.loc[ TOTAL ] = csmposting.sum(axis=0).astype(int)

Apart from that I don t see anything wrong with the code.

这里使用Pandas的样本代码重复了两种计算结果,似乎可以进行罚款。

import pandas as pd

df = pd.DataFrame(
    [[779, 285, 16, 2, 3],
     [290, 20, 3, 3, 0],
     [0, 0, 3, 7, 0],
     [547, 97, 8, 10, 0]],
    index=[ Unit1 ,  Unit2 ,  Unit3 ,  Unit4 ],
    columns=[ c1 ,  c2 ,  c3 ,  c4 ,  c5 ]
)
print(df)  # similar to your sample data in `csmposting`
#         c1   c2  c3  c4  c5
# Unit1  779  285  16   2   3
# Unit2  290   20   3   3   0
# Unit3    0    0   3   7   0
# Unit4  547   97   8  10   0

df[ TOTAL ] = df.sum(axis=1)
df.loc[ TOTAL ] = df.sum(axis=0)
print(df)
#          c1   c2  c3  c4  c5  TOTAL
# Unit1   779  285  16   2   3   2170
# Unit2   290   20   3   3   0    632
# Unit3     0    0   3   7   0     20
# Unit4   547   97   8  10   0   1324
# TOTAL  3232  804  60  44   6   8292

Please tell us more what error you get. It might also be helpful to see csmposting.index and csmposting.columns and csmposting.isna() to check for missing values.





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签