我的数据框如下:
POSITIVE NEGATIVE MASTER Net Positivity
Speaker
Mike 36.0 29.0 2187.0 0.32%
Rob 36.0 35.0 2413.0 0.04%
May 2.0 4.0 369.0 -0.54%
我想在末尾添加一行,总行数为正(36 + 36 + 2),总行数为负(29 + 35 + 4),总行数(2187 + 2413 + 369)。新行的索引应为“ TOTAL”。总净阳性为(总阳性-阴性总)/总MASTER。
POSITIVE NEGATIVE MASTER Net Positivity
Speaker
Mike 36.0 29.0 2187.0 0.32%
Rob 36.0 35.0 2413.0 0.04%
May 2.0 4.0 369.0 -0.54%
TOTAL 74.0 68.0 4969.0 0.12%
我应该如何在熊猫中添加总行数?
答案 0 :(得分:1)
您可以简单地
DateTime startTimeStr = DateTime.Parse("2020-04-21T21:44:34Z");
答案 1 :(得分:1)
尝试一下:
df = df.append(df.sum(numeric_only=True), ignore_index=True)
df['Net_Positivity'] = (df['POSITIVE']-df['NEGATIVE'])*100/df['MASTER']
df['Speaker'].fillna('Total', inplace=True)
df.set_index('Speaker', inplace=True)
print(df)
POSITIVE NEGATIVE MASTER Net_Positivity
Speaker
Mike 36.0 29.0 2187.0 0.320073
Rob 36.0 35.0 2413.0 0.041442
May 2.0 4.0 369.0 -0.542005
Total 74.0 68.0 4969.0 0.120749
答案 2 :(得分:1)
您可以使用loc
在df上添加行和sum
。然后以正确的格式计算净正数
df.loc['total'] = df.iloc[:, :-1].sum()
df.loc['total', 'Net Positivity'] = f"""{(df.loc['total', 'POSITIVE']
- df.loc['total', 'NEGATIVE'])
/df.loc['total', 'MASTER']:.2%}"""
POSITIVE NEGATIVE MASTER Net Positivity
Speaker
Mike 36.0 29.0 2187.0 0.32%
Rob 36.0 35.0 2413.0 0.04%
May 2.0 4.0 369.0 -0.54%
total 74.0 68.0 4969.0 0.12%