简体   繁体   English

python熊猫多列总和

[英]python pandas multiple column sum

Need some advise: I am trying to SUM the SERIAL COUNT field with the total of SERIAL NUMBERS in a GROUP of the same ITEM NUMBER?需要一些建议:我正在尝试将序列号字段与同一项目编号的组中的序列号总数相加? Any suggestions welcome?欢迎任何建议?

 fincon_serials = pd.read_csv('Raw Data/LISTSNO Calvinia.TXT',skiprows = 1)

 fincon_codes = pd.read_excel('Raw Data/STKCOUNT Calvinia.xls', index=None)

 fincon_codes = fincon_codes.drop(columns=['GROUP','BIN','Unnamed: 5'])

 fincon_codes = pd.merge(fincon_codes, fincon_serials[['ITEM NUMBER', 'SERIAL NUMBER']], on = ['ITEM NUMBER'])

 fincon_codes['SERIAL COUNT'] = 1

 fincon_codes = fincon_codes.groupby(['ITEM NUMBER','CAT','DESCRIPTION','NORMAL','FAULTY','SUPPLR','SWP-OUT','TOTAL','COST','VALUE','SERIAL NUMBER'])[['SERIAL COUNT']].sum()

 fincon_codes.head()

This is my first question, so please accept my apologies if it is not in the correct format.这是我的第一个问题,如果格式不正确,请接受我的道歉。

I have searched for solution on the PANDAS documentation, but cannot find a solution and suits that which I want to do as the SERIAL COUNT column should count the SERIALS in THE SERIAL NUMBER column and add that to the SERIAL COUNT.我已经在 PANDAS 文档中搜索了解决方案,但找不到适合我想要做的解决方案,因为 SERIAL COUNT 列应该计算 THE SERIAL NUMBER 列中的 SERIALS 并将其添加到 SERIAL COUNT。

Current output:电流输出:

代码输出

What is needed is that the SERIAL COUNT should have a total for each item number group.需要的是 SERIAL COUNT 应该有每个项目编号组的总数。

Thank you谢谢

I have created some data for example:我创建了一些数据,例如:

item_number = pd.Series([1,2,2,1,1,3])
serial_number = pd.Series([112, 111, 111, 134, 155, 111])
another_column = pd.Series([1,2,3,4,5,6])
df = pd.concat([item_number, serial_number, another_column], 1)
df.columns = ['item_number', 'serial_number', 'another_column']

In your data, "another column" is any another column like "CAT", "DESCROPTION", etc. If you need to calculate count of unique values "serial number" in "item number" I think you can just use groupby(['item_number', 'serial_number']) .在您的数据中,“另一列”是任何另一列,如“CAT”、“DESCROPTION”等。如果您需要计算“项目编号”中唯一值“序列号”的数量,我认为您可以使用groupby(['item_number', 'serial_number'])

result = df.groupby(['item_number', 'serial_number']).another_column.count()

And you will have output like this:你将有这样的输出:

item_number  serial_number
1            112              1
             134              1
             155              1
2            111              2
3            111              1

Do you need concatenate this with other columns?您是否需要将其与其他列连接? If you can present a peace of your data, then I can write full example.如果您可以提供一个和平的数据,那么我可以编写完整的示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM