我有数据框:
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:text="Name"
android:theme="@android:style/Theme.Holo"
android:ems="10"
android:id="@+id/editText"
android:layout_below="@+id/textView"
android:layout_centerHorizontal="true" />
我需要计算每ID,"url","app_name","used_at","active_seconds","device_connection","device_os","device_type","device_usage"
1ca9bb884462c3ba2391bf669c22d4bd,"",VK Client,2016-01-01 00:00:13,5,3g,ios,smartphone,home
b8f4df3f99ad786a77897c583d98f615,"",VKontakte,2016-01-01 00:01:45,107,wifi,android,smartphone,home
1ca9bb884462c3ba2391bf669c22d4bd,"",Twitter,2016-01-01 00:02:48,20,3g,ios,smartphone,home
1ca9bb884462c3ba2391bf669c22d4bd,"",VK Client,2016-01-01 00:03:08,796,3g,ios,smartphone,home
b8f4df3f99ad786a77897c583d98f615,"",WhatsApp Messenger,2016-01-01 00:03:32,70,wifi,android,smartphone,home
b8f4df3f99ad786a77897c583d98f615,"",VKontakte,2016-01-01 00:04:42,27,wifi,android,smartphone,home
b8f4df3f99ad786a77897c583d98f615,"",VKontakte,2016-01-01 00:05:30,5,wifi,android,smartphone,home
b8f4df3f99ad786a77897c583d98f615,"",WhatsApp Messenger,2016-01-01 00:05:36,47,wifi,android,smartphone,home
b8f4df3f99ad786a77897c583d98f615,"",VKontakte,2016-01-01 00:06:23,20,wifi,android,smartphone,home
a703114aa8a03495c3e042647212fa63,"",Instagram,2016-01-01 00:06:41,118,3g,android,smartphone,home
1637ce5a4c4868e694004528c642d0ac,"",Camera,2016-01-01 00:06:43,16,wifi,android,smartphone,home
1637ce5a4c4868e694004528c642d0ac,"",VKontakte,2016-01-01 00:07:00,45,wifi,android,smartphone,home
a703114aa8a03495c3e042647212fa63,"",VKontakte,2016-01-01 00:08:40,99,3g,android,smartphone,home
1637ce5a4c4868e694004528c642d0ac,"",VKontakte,2016-01-01 00:10:05,1,wifi,android,smartphone,home
到每app_name
的份额。
但我接下来不能做:
每个应用程序与每个ID的总和我应该除以所有应用程序到id和下一个100的总和。(找到百分比)
我这样做:
ID
但是当我尝试
时,它只会向每个应用返回数量short = df.groupby(['ID', 'app_name']).agg({'app_name': len, 'active_seconds': sum}).rename(columns={'active_seconds': 'count_sec', 'app_name': 'sum_app'}).reset_index()
它会返回错误
我该如何解决?
答案 0 :(得分:3)
你需要的IIUC:
short = df.groupby(['ID', 'app_name'])
.agg({'app_name': len,
'active_seconds': lambda x: 100 * x.sum() / df.active_seconds.sum()})
.rename(columns={'active_seconds': 'count_sec', 'app_name': 'sum_app'})
.reset_index()
print (short)
ID app_name count_sec sum_app
0 1637ce5a4c4868e694004528c642d0ac Camera 1.162791 1
1 1637ce5a4c4868e694004528c642d0ac VKontakte 3.343023 2
2 1ca9bb884462c3ba2391bf669c22d4bd Twitter 1.453488 1
3 1ca9bb884462c3ba2391bf669c22d4bd VK Client 58.212209 2
4 a703114aa8a03495c3e042647212fa63 Instagram 8.575581 1
5 a703114aa8a03495c3e042647212fa63 VKontakte 7.194767 1
6 b8f4df3f99ad786a77897c583d98f615 VKontakte 11.555233 4
7 b8f4df3f99ad786a77897c583d98f615 WhatsApp Messenger 8.502907 2
另一种解决方案:
#you need another name of df, e.g. short1
short1 = df.groupby(['ID', 'app_name'])
.agg({'app_name': len, 'active_seconds': sum})
.rename(columns={'active_seconds': 'count_sec', 'app_name': 'sum_app'})
.reset_index()
short1.count_sec = 100 * short1.count_sec / df.active_seconds.sum()
print (short1)
ID app_name count_sec sum_app
0 1637ce5a4c4868e694004528c642d0ac Camera 1.162791 1
1 1637ce5a4c4868e694004528c642d0ac VKontakte 3.343023 2
2 1ca9bb884462c3ba2391bf669c22d4bd Twitter 1.453488 1
3 1ca9bb884462c3ba2391bf669c22d4bd VK Client 58.212209 2
4 a703114aa8a03495c3e042647212fa63 Instagram 8.575581 1
5 a703114aa8a03495c3e042647212fa63 VKontakte 7.194767 1
6 b8f4df3f99ad786a77897c583d98f615 VKontakte 11.555233 4
7 b8f4df3f99ad786a77897c583d98f615 WhatsApp Messenger 8.502907 2