Pandas返回对象类型而不是值

时间:2017-05-08 17:47:35

标签: python pandas

我有以下代码:

import pandas as pd
df=pd.read_csv('Fortigate-Inbound traffic from blacklisted IP.csv')
df2= df[df['Device Action']=='Accept']
df3 = df2.groupby(['Destination Address', 'Sum(Aggregated Event Count)'])
print(df3)

返回pandas.core.groupby.DataFrameGroupBy对象0x0000016F627C3208,而不是数据帧中的实际值。如何才能打印出值?

1 个答案:

答案 0 :(得分:0)

我认为sum需要aggregation

df3 = df2.groupby('Destination Address', as_index=False)['Aggregated Event Count'].sum()

样品:

df2 = pd.DataFrame({'Destination Address':['a','a','a', 'b'],
                   'Aggregated Event Count':[1,2,3,4]})
print (df2)
   Aggregated Event Count Destination Address
0                       1                   a
1                       2                   a
2                       3                   a
3                       4                   b

df3 = df2.groupby('Destination Address', as_index=False)['Aggregated Event Count'].sum()
print (df3)
  Destination Address  Aggregated Event Count
0                   a                       6
1                   b                       4

另一种解决方案:

df3 = df2.groupby('Destination Address')['Aggregated Event Count'].sum().reset_index()
print (df3)
  Destination Address  Aggregated Event Count
0                   a                       6
1                   b                       4