相关疑难解决方法(0)

如何按键访问pandas groupby dataframe

如何通过密钥访问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)

python group-by dataframe pandas pandas-groupby

134
推荐指数
5
解决办法
16万
查看次数

为什么数据库行元组中的整数后缀为'L'?

我的问题是为什么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.

我只是好奇为什么整数会以这种方式出现.

python mysql

46
推荐指数
3
解决办法
4万
查看次数

Pandas to_dict 类似列表的输出

我有一个像这样的数据框,

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方法,谢谢。

python dictionary pandas

3
推荐指数
1
解决办法
89
查看次数