我已成功使用groupby函数按组对给定变量求和或平均,但有没有办法聚合成值列表,而不是获得单个结果?(这还会被称为聚合吗?)
我不完全确定这是我应该采取的方法,所以下面是我想用玩具数据进行转换的一个例子.
也就是说,如果数据看起来像这样:
A B C
1 10 22
1 12 20
1 11 8
1 10 10
2 11 13
2 12 10
3 14 0
Run Code Online (Sandbox Code Playgroud)
我想要最终得到的是以下内容.我不能完全确定这是否可以通过groupby聚合到列表中来完成,而且相对于从这里去哪里感到很遗憾.
假设输出:
A B C New1 New2 New3 New4 New5 New6
1 10 22 12 20 11 8 10 10
2 11 13 12 10
3 14 0
Run Code Online (Sandbox Code Playgroud)
也许我应该追求支点呢?将数据放入列的顺序无关紧要 - 本例中的所有列B到New6都是等效的.所有建议/更正都非常感谢.
我正在使用我已经在我的工作中使用了很长时间的聚合函数.这个想法是,如果系列传递给函数的长度为1(即该组只有一个观察值),则返回该观察值.如果传递的系列的长度大于1,则在列表中返回观察结果.
这对某些人来说可能看起来很奇怪,但这不是X,Y问题,我有充分的理由想要这样做与这个问题无关.
这是我一直在使用的功能:
def MakeList(x):
""" This function is used to aggregate data that needs to be kept distinc within multi day
observations for later use and transformation. It makes a list of the data and if the list is of length 1
then there is only one line/day observation in that group so the single element of the list is returned.
If the list is longer than one then there are multiple line/day observations and the list itself is …Run Code Online (Sandbox Code Playgroud) 我有一个数据框,其中有多个用户条目。这些用户还可以分配给多个 ID。
我想按用户进行分组,然后将这些 ID 的列表存储在另一列中,如下所示:
我想从这里开始:
df1 = pd.DataFrame({'USER': ['BOB','STEVE','PAUL','KEITH','STEVE','STEVE','BOB'],'ID':[1,2,3,4,5,6,7]})
Run Code Online (Sandbox Code Playgroud)
对此。仅当该用户附加到多个 ID 时才显示值
给出以下内容 df
Id other concat
0 A z 1
1 A y 2
2 B x 3
3 B w 4
4 B v 5
5 B u 6
Run Code Online (Sandbox Code Playgroud)
我希望结果包含new带有分组值的列作为列表
Id other concat new
0 A z 1 [1, 2]
1 A y 2 [1, 2]
2 B x 3 [3, 4, 5, 6]
3 B w 4 [3, 4, 5, 6]
4 B v 5 [3, 4, 5, 6]
5 B u 6 [3, 4, 5, 6] …Run Code Online (Sandbox Code Playgroud) 我有一个数据框df和一列,df['table']这样每个项目df['table']都是另一个具有相同标题/列数的数据框。我想知道是否有办法做groupby这样的事情:
原始数据框:
name table
Bob Pandas df1
Joe Pandas df2
Bob Pandas df3
Bob Pandas df4
Emily Pandas df5
Run Code Online (Sandbox Code Playgroud)
分组后:
name table
Bob Pandas df containing the appended df1, df3, and df4
Joe Pandas df2
Emily Pandas df5
Run Code Online (Sandbox Code Playgroud)
我发现这个代码片段可以groupby对数据帧中的字符串执行 a和 lambda,但无法弄清楚如何将整个数据帧附加到groupby.
name table
Bob Pandas df1
Joe Pandas df2
Bob Pandas df3
Bob Pandas df4
Emily Pandas df5
Run Code Online (Sandbox Code Playgroud)
我也试过df['table'] = df.groupby(['name'])['HTML'].apply(list),但给了我df['table']所有的NaN。
谢谢你的帮助!!
我有一个使用pandas的DataFrame:
one two three
1 2 1
4 1 1
2 2 1
3 1 2
20 2 2
Run Code Online (Sandbox Code Playgroud)
现在,我将通过分组'三'来提取a向量.基本上,我应该根据"三个"分组从"两个"列中获取向量:
groupby('three')
a=[2,1,2]
b=[1,2]
Run Code Online (Sandbox Code Playgroud)
非常感谢
I'm trying to combine multiple rows of a dataframe into one row, with the columns with different values being combined in a list. There are multiple columns with different values.
The df.groupby('a')['b'].apply(list) works well if only 1 column ('b' in this instance) has to be made to a list, but I can't figure out how to do it for multiple columns.
Dataframe:
a b c d
0 1 b 1 first
1 1 b 2 second
2 2 c 1 …Run Code Online (Sandbox Code Playgroud) 这是一个示例数据框:
label data
a 1.09
b 2.1
a 5.0
b 2.0
c 1.9
Run Code Online (Sandbox Code Playgroud)
我想要的是
arr = [[1.09, 5.0], [2.1, 2.0],[1.9]]
Run Code Online (Sandbox Code Playgroud)
最好是一个 numpy 数组列表。
我知道这df.groupby.groups.keys()给了我列表['a','b','c'],并df.groupby.groups.values()给了我类似的东西arr,但作为一个Int64Index对象。但是,我试过了df.loc[df.groupby.groups.values()]['label'],并没有得到想要的结果。
我该如何实现?谢谢!
我有这样的df:
ID Cluster Product
1 4 'b'
1 4 'f'
1 4 'w'
2 7 'u'
2 7 'b'
3 5 'h'
3 5 'f'
3 5 'm'
3 5 'd'
4 7 's'
4 7 'b'
4 7 'g'
Run Code Online (Sandbox Code Playgroud)
其中ID是另一个df的主键和唯一键,该df是此df的源.群集不是关键,不同的ID通常具有相同的群集值; 无论如何,这是我必须要进行的信息.
我想要获得的是这个数据帧:
ID Cluster Product_List_by_ID
1 4 ['b','f','w']
2 7 ['u','b']
3 5 ['h','f','m','d']
4 7 ['s','b','g']
Run Code Online (Sandbox Code Playgroud)
如果这是不可能的,那么像这样的字典也可以:
d = {ID:[1,2,3,4], Cluster:[4,7,5,7],
Product_List_by_ID:[['b','f','w'],['u','b'],['h','f','m','d'],['s','b','g']]}
Run Code Online (Sandbox Code Playgroud)
我尝试了很多方法都没有成功..似乎不可能将列表作为pandas数据帧值插入..无论如何,我认为以某种棘手的方式获取目标应该不会那么困难...对不起,如果我要走出去记住,但我是编码的新手
有什么建议吗?!谢谢
我确定这已经被问过了,如果重复的话,对不起。假设我有以下数据框:
df = pd.DataFrame({'key': ['A', 'B', 'C', 'A', 'B', 'C'],
'data': range(6)}, columns=['key', 'data'])
>>
key data
0 A 0
1 B 1
2 C 2
3 A 3
4 B 4
5 C 5
Run Code Online (Sandbox Code Playgroud)
对“键”进行分组,df.groupby('key').sum()我知道我们可以做以下事情:
>>
data
key
A 3
B 5
C 7
Run Code Online (Sandbox Code Playgroud)
获取数组中所有“拆分”数据的最简单方法是什么?
>>
data
key
A [0, 3]
B [1, 4]
C [2, 5]
Run Code Online (Sandbox Code Playgroud)
我不一定只按一个键进行分组,但也要按其他几个索引进行分组(例如“年”和“月”),这就是为什么我想使用groupby函数,但将所有分组值保留在一个键中的原因。数组。