0
我需要建立从数据框(“分组”)建成的组中选定值的字典。 idcolumn是一个字符串列表[“column_name”](我把它作为一个列表,因为在某些时候,我需要为使用标签的操作添加各种其他字符串/列名称)。奇怪groupby /数据帧行为与列表()
所以用作平均来检索组这样的说法:
grouped.get_group(k).loc[:,idcolumn]
其中相同作品完美到
grouped.get_group(k).loc[:,idcolumn[0]]
输出所选择的数据的数据帧以[“栏”]作为头。
我的完整表述是:
dict_to_build= {k: list(grouped.get_group(k).loc[:,idcolumn]) for k in grouped.groups.keys() }
,但我有一个非常奇怪的错误。
构建的字典包含所有键但是作为唯一值“column_name”。 虽然如果我使用
dict_to_build= {k: list(grouped.get_group(k).loc[:,idcolumn[0]) for k in grouped.groups.keys() }
的字典是完全确定。
系为例这样的:
In [115]: pde=pd.DataFrame({"a":[1,2,3,1,2,3], "column_name":["a","b","c","d","e","f"]})
In [116]: pde
Out[116]:
a column_name
0 1 a
1 2 b
2 3 c
3 1 d
4 2 e
5 3 f
In [117]: grouped=pde.groupby[1]
Traceback (most recent call last):
File "<ipython-input-117-b504dadfee12>", line 1, in <module>
grouped=pde.groupby[1]
TypeError: 'method' object is not subscriptable
In [118]: grouped=pde.groupby("a")
In [119]: grouped.get_group(1).loc[:,"column_name"]
Out[119]:
0 a
3 d
Name: column_name, dtype: object
In [120]: list(grouped.get_group(1).loc[:,"column_name"])
Out[120]: ['a', 'd']
In [121]: list(grouped.get_group(1).loc[:,["column_name"]])
Out[121]: ['column_name']
有人能赐教大约发生了什么?这对我来说很奇怪。问题来自数据框,带有list作为参数的loc,还是列表函数?
非常有意义;)非常感谢! –
不客气! – Allen