[英]Pandas DF: Sum rows of numbers cast as strings into a new column
Ok, I'm stumped and have surfed all available pages, followed it all, and still ugh! 好的,我很困惑,已经浏览了所有可用的页面,全部浏览了,仍然很丑! So here is my df. 这是我的df。 I want to add all the values in row 4 together and have a row total at end, then do it for 5, 6, 7... I use .concat()
and .sum()
and get key errors or too many arguments, tried .groupby
, and even .add()
(worth a try) as the columns are 'lists', nothing gives totals. 我想将第4行中的所有值加在一起,并在末尾总计一行,然后对5、6、7进行...我使用.concat()
和.sum()
并得到键错误或参数过多,尝试了.groupby
甚至是.add()
(值得一试),因为这些列是“列表”,没有任何东西可以得出总数。
1day 2day 3day 4day 5day 6day 7day
4 2.979 2.979 2.979 2.979 2.979 2.979 2.979
5 9.543 9.543 9.543 9.543 9.543 9.543 9.543
6 5.222 5.222 5.222 5.222 5.222 5.222 5.222
7 0.319 0.319 0.319 0.319 0.319 0.319 0.319
10 -4.491 -4.491 -4.491 -4.491 -4.491 -4.491 -4.491
14 2.178 2.178 2.178 2.178 2.178 2.178 2.178
15 7.507 7.507 7.507 7.507 7.507 7.507 7.507
16 0.612 0.612 0.612 0.612 0.612 0.612 0.612
17 4.488 4.488 4.488 4.488 4.488 4.488 4.488
I had some luck with df.groupby(df.index)[['1day','2day'...'7day'].sum()
but it just pushes them together, not adds them. 我对df.groupby(df.index)[['1day','2day'...'7day'].sum()
感到幸运,但它只是将它们推到一起,而不是相加。 (I am aware that all the values are equal) In excel, sum(a1:g1) but in pandas, I'm just writing into a deeper hole, please help! (我知道所有值都相等)在excel中,sum(a1:g1)但是在熊猫中,我只是在写一个更深的洞,请帮忙! screen shot of df, may be a better view df的屏幕截图,可能是更好的视图
.sum()
applied to DF of strings .sum()
应用于字符串的DF Without Altering the original Q&A... Here is an updated answer for your revised question: 在不更改原始问答的情况下...这是您修改后的问题的更新答案:
The issue is that you'll need to convert the strings to integers. 问题是您需要将字符串转换为整数。 That just makes it a two step process. 这只是一个两步过程。 I'll show all the steps so it's clear: 我将显示所有步骤,这样很清楚:
In [83]: str_lst =[[ '7', '3', '2', '-1', '5', '8', '0'],
[ '6', '8', '4', '0', '8', '7', '6'],
[ '6', '1', '2', '8', '6', '-1', '2'],
[ '0', '-1', '3', '5', '1', '3', '7'],
[ '6', '5', '2', '8', '4', '2', '3'],
[ '4', '-1', '-1', '3', '2', '3', '-1'],
[ '8', '6', '6', '0', '8', '0', '0'],
['-1', '3', '3', '0', '4', '2', '4'],
[ '2', '1', '2', '7', '0', '8', '5'],
[ '3', '8', '1', '0', '8', '1', '8']]
In [85]: df = pd.DataFrame(str_lst, columns=['1day', '2day',
'3day', '4day', '5day', '6day', '7day'])
In [93]: df = df.apply(pd.to_numeric)
In [94]: df['Totals'] = df.sum(axis =1)
If you do the total before the pd.to_numeric
it is the summation of strings. 如果在pd.to_numeric
之前进行pd.to_numeric
,则为字符串的总和。 If you do the total after teh pd.to_numeric
it is the summation of integers. 如果在pd.to_numeric
之后进行pd.to_numeric
,则为整数的总和。
After you've created your DF. 创建DF之后。 Try: 尝试:
df['Totals'] = df.sum(axis=1)
This will add a column at the far right with a sum of the rows. 这将在最右边添加一列,其中包含行的总和。
In [63]: df = pd.DataFrame(np.random.randint(-1,9,size=(10, 7)),
columns=['1day', '2day', '3day', '4day',
'5day', '6day', '7day'])
In [64]: df
Out[64]:
1day 2day 3day 4day 5day 6day 7day
0 0 0 7 -1 2 2 -1
1 0 3 3 2 -1 3 6
2 0 8 0 -1 8 -1 1
3 3 8 -1 2 6 3 8
4 5 0 1 8 8 -1 3
5 7 8 1 4 8 1 8
6 5 7 5 -1 7 -1 1
7 6 3 2 2 8 1 5
8 3 7 3 3 3 -1 1
9 2 -1 8 1 1 0 5
In [65]: df['Totals'] = df.sum(axis=1)
In [66]: df
Out[66]:
1day 2day 3day 4day 5day 6day 7day Totals
0 0 0 7 -1 2 2 -1 9
1 0 3 3 2 -1 3 6 16
2 0 8 0 -1 8 -1 1 15
3 3 8 -1 2 6 3 8 29
4 5 0 1 8 8 -1 3 24
5 7 8 1 4 8 1 8 37
6 5 7 5 -1 7 -1 1 23
7 6 3 2 2 8 1 5 27
8 3 7 3 3 3 -1 1 19
9 2 -1 8 1 1 0 5 16
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.