【问题标题】:iterate selection over df pandas对 df pandas 进行迭代选择
【发布时间】:2015-06-25 19:40:16
【问题描述】:

我有一个从 JSON 转换而来的 pandas DataFrame,并提取了我想要的列和行(很好地导出到 CSV)。

但是,我想进入几个“行中的级别”。我想使用类似的东西来迭代我的索引数据的选择:

df.likes[0]['count']

我的 DataFrame 被称为 dflikes 是列。我想访问整行的“count”值,将其打印回 DataFrame 的相同位置和列。

这是数据:

>>> df_likes.ix[0:94]
0     {u'count': 1685, u'anchorStr': u'1225924030067...
1     {u'count': 1944, u'anchorStr': u'1225924022404...
2     {u'count': 2586, u'anchorStr': u'1225924194077...
3     {u'count': 3075, u'anchorStr': u'1225924298746...
4     {u'count': 3263, u'anchorStr': u'1225924358464...
5     {u'count': 3390, u'anchorStr': u'1225924289589...
6     {u'count': 4306, u'anchorStr': u'1225924191359...
7     {u'count': 751, u'anchorStr': u'12259244210391...
8     {u'count': 2425, u'anchorStr': u'1225924419831...
9     {u'count': 2628, u'anchorStr': u'1225924362763...
10    {u'count': 1521, u'anchorStr': u'1225924213802...

【问题讨论】:

  • “将其打印回相同的位置和列”到底是什么意思?
  • 如果可能的话,您能否提供一个简短的输入 JSON 文件的 sn-p?我不确定,如果你正确地导入了你的数据,因为你的 DataFrame(甚至可能是一个系列)看起来有点奇怪,只是包含你的 JSON 文件的这些“subdicts”。

标签: python loops pandas selection rows


【解决方案1】:

假设您想通过使用其索引和列名来访问单元格值,以下示例代码根据docs 完成这项工作:

#!/usr/bin/env python3
# coding: utf-8

from pandas import DataFrame
from numpy.random import randn

df = DataFrame(randn(8, 4), columns=['A', 'B', 'C', 'D'])

#access column with name 'A' and index 6
print(df['A'].ix[6])

【讨论】:

    【解决方案2】:

    也许你是这样想的?

        anchorStr        ucount
    0   1225924030067    1685
    1   1225924022404    1944
    2   1225924194077    2586
    3   1225924298746    3075
    4   1225924358464    3263
    5   1225924289589    3390
    6   1225924191359    4306
    7   1225924421039     751
    8   1225924419831    2425
    9   1225924362763    2628
    10  1225924213802    1521
    

    如果你这么想,这段代码就可以了。

    from pandas import DataFrame as df
    
    data = [
    {u'count': 1685, u'anchorStr': u'1225924030067'},
    {u'count': 1944, u'anchorStr': u'1225924022404'},
    {u'count': 2586, u'anchorStr': u'1225924194077'},
    {u'count': 3075, u'anchorStr': u'1225924298746'},
    {u'count': 3263, u'anchorStr': u'1225924358464'},
    {u'count': 3390, u'anchorStr': u'1225924289589'},
    {u'count': 4306, u'anchorStr': u'1225924191359'},
    {u'count': 751, u'anchorStr': u'1225924421039'},
    {u'count': 2425, u'anchorStr': u'1225924419831'},
    {u'count': 2628, u'anchorStr': u'1225924362763'},
    ...
    {u'count': 1521, u'anchorStr': u'1225924213802'}]
    
    c = [c['count'] for c in d]
    a = [a['anchorStr'] for a in d]
    
    d = df({'ucount': c, 'anchorStr': a})
    
    print d
    

    很高兴能帮上忙!如果您觉得我的回答对您有用,请随时接受。 :-)

    【讨论】:

      猜你喜欢
      • 2019-05-02
      • 2017-04-22
      • 2022-01-12
      • 1970-01-01
      • 2017-01-29
      • 2016-04-16
      • 2017-03-10
      • 2015-08-22
      • 1970-01-01
      相关资源
      最近更新 更多