需要帮助将两列的唯一组合添加到pandas中的同一数据框中。 我想要那个“nos”专栏。
Input:
id acct_nos name
1 1a one
1 1a two
2 2b three
3 3a four
3 3b five
3 3c six
3 3d seven
这是我想要的输出:
Output:
id acct_nos nos name
1 1a 1 one
1 1a 1 two
2 2b 1 three
3 3a 4 four
3 3b 4 five
3 3c 4 six
3 3d 4 seven
在上面的例子中 Id = 1只有1个acct_nos-1a,因此nos必须具有值1。 Id = 3只有4个acct_nos-3a到3d,因此nos必须具有值4。
不确定如何将它放在Python Pandas中。我可以搞清楚的SQL查询。
由于
答案 0 :(得分:6)
您可以Environment:
Request Method: GET
Request URL: http://127.0.0.1:8000/admin/test%3D5/
Django Version: 1.10.5
Python Version: 2.7.12
Installed Applications:
['alexbase',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'easy_thumbnails']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware']
Traceback:
File "/home/user/.local/lib/python2.7/site-packages/django/core/handlers/exception.py" in inner
39. response = get_response(request)
File "/home/user/.local/lib/python2.7/site-packages/django/core/handlers/base.py" in _get_response
187. response = self.process_exception_by_middleware(e, request)
File "/home/user/.local/lib/python2.7/site-packages/django/core/handlers/base.py" in _get_response
185. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/user/.local/lib/python2.7/site-packages/django/contrib/auth/decorators.py" in _wrapped_view
23. return view_func(request, *args, **kwargs)
Exception Type: TypeError at /admin/test=5/
Exception Value: object() takes no parameters
使用groupby.transform
功能计算每个 id 的唯一元素数量:
nunique()
答案 1 :(得分:2)
选项1
df.assign(nos=df.id.map(df.drop_duplicates(['id', 'acct_nos']).id.value_counts()))
选项2
使用Counter
from collections import Counter
tups = pd.unique(
zip(df.id.values.tolist(), df.acct_nos.values.tolist())
).tolist()
df.assign(nos=df.id.map(Counter([tup[0] for tup in tups])))
id acct_nos name nos
0 1 1a one 1
1 1 1a two 1
2 2 2b three 1
3 3 3a four 4
4 3 3b five 4
5 3 3c six 4
6 3 3d seven 4