如何通过密钥访问groupby对象中的相应groupby数据帧?使用以下groupby:
rand = np.random.RandomState(1)
df = pd.DataFrame({'A': ['foo', 'bar'] * 3,
'B': rand.randn(6),
'C': rand.randint(0, 20, 6)})
gb = df.groupby(['A'])
Run Code Online (Sandbox Code Playgroud)
我可以遍历它以获取密钥和组:
In [11]: for k, gp in gb:
print 'key=' + str(k)
print gp
key=bar
A B C
1 bar -0.611756 18
3 bar -1.072969 10
5 bar -2.301539 18
key=foo
A B C
0 foo 1.624345 5
2 foo -0.528172 11
4 foo 0.865408 14
Run Code Online (Sandbox Code Playgroud)
我希望能够做类似的事情
In [12]: gb['foo']
Out[12]:
A B C
0 foo 1.624345 5
2 foo …Run Code Online (Sandbox Code Playgroud) 我的问题是为什么MySQL行的整数值后缀为'L'?以下是详细信息:
以下词典 - 为了便于展示而人为地格式化 -
{'estimated': '',
'suffix': '',
'typeofread': 'g',
'acct_no': 901001000L,
'counter': 0,
'time_billed': datetime.datetime(2012, 5, 1, 9, 5, 33),
'date_read': datetime.datetime(2012, 3, 13, 23, 19, 45),
'reading': 3018L,
'meter_num': '26174200'}
Run Code Online (Sandbox Code Playgroud)
由一个MySQL数据库表的列压缩而成,其中包含从表中读取一次的结果.
我可以通过将这些值传递给int()来删除'L',所以如果该字典位于名为snapped_read的变量中,我可以这样做:
int(snapped_read['reading'])并将3018L改为3018.
我只是好奇为什么整数会以这种方式出现.
我有一个像这样的数据框,
Roll No date status Name
1 1/1/2020 on A
2 1/1/2020 on A
3 1/1/2020 on B
Run Code Online (Sandbox Code Playgroud)
我正在尝试创建一个字典,其中键是名称,值是卷号列表,我无法处理重复的名称,
我的预期输出是
{
"A" :[1,2],
"B" :[3]
}
Run Code Online (Sandbox Code Playgroud)
我可以通过迭代数据框来获取输出,我正在寻找一种pandorable方法,谢谢。