【问题标题】:How can I iterate specific rows in a dataframe?如何迭代数据框中的特定行?
【发布时间】:2021-11-17 12:06:08
【问题描述】:

我想根据Content_ID_group 列中的列表因子数迭代特定行。

例如,由于下表的第一行在Content_ID_group 列的列表中有六个因子,我想让其他列(Event NameMedia)迭代六次。

所以作为我拥有的原始数据框:

>>> import pandas as pd
>>> graph = pd.DataFrame({"Event Name": ["ord", "inflow"], "Media":["google", "appier"], "Content Id_group":[[1,2,3,4,5,6], 0]})
>>> print(graph)
  Event Name   Media    Content Id_group
0        ord  google  [1, 2, 3, 4, 5, 6]
1     inflow  appier                   0

这是我想要的输出:

  Event Name   Media Content Id_group
0        ord  google                1
0        ord  google                2
0        ord  google                3
0        ord  google                4
0        ord  google                5
0        ord  google                6
1     inflow  appier                0

这是我的代码。它只是继续运行,而不是给出结果。所以我猜这是一个无限循环。

有人可以帮忙吗?

  • graph 是我定义的图表。
  • g_whole 是必须包含我定义的所有迭代结果的数据框。
g_whole=pd.DataFrame()

for g in range(len(graph)):
    if type(graph['Content ID_group'][0]) == list:
        loop=len(graph['Content ID_group'][0])
        for l in range(loop):
            itt=graph.loc[g]
            g_whole.append(itt)
        
    elif type(graph['Content ID_group'][0]) == int:
        g_whole.append(graph['Content ID_group'][0])
print(g_whole)

【问题讨论】:

  • 您需要检查建议的答案,如果他们解决了您的问题,请批准或提供反馈

标签: python pandas dataframe numpy iteration


【解决方案1】:

使用pd.DataFrame.explode():

In [1]: df = pd.DataFrame({'Event Name': ['ord', 'inflow'], 'Media': ['google', 'appier'], 'Content ID_group': [[1,2,3,4,5,6], 0]})
In [2]: df.explode('Content ID_group')
Out[2]:
  Event Name   Media Content ID_group
0        ord  google                1
0        ord  google                2
0        ord  google                3
0        ord  google                4
0        ord  google                5
0        ord  google                6
1     inflow  appier                0

【讨论】:

    【解决方案2】:
    import pandas as pd
    
    graph = pd.DataFrame({"Event Name": ["ord", "inflow"], "Media":["google", "appier"], "Content Id_group":[[1,2,3,4,5,6], 0]})
    
    print(graph)
      Event Name   Media    Content Id_group
    0        ord  google  [1, 2, 3, 4, 5, 6]
    1     inflow  appier                   0
    
    g_whole = graph.explode("Content Id_group")
    
    print(g_whole)
      Event Name   Media Content Id_group
    0        ord  google                1
    0        ord  google                2
    0        ord  google                3
    0        ord  google                4
    0        ord  google                5
    0        ord  google                6
    1     inflow  appier                0
    
    

    【讨论】:

      【解决方案3】:

      假设Content ID_group包含一个真实的列表,你可以使用explode

      >>> type(graph.at[0, 'Content ID_group'])
      list
      
      >>> graph.explode('Content ID_group')
        Event Name   Media Content ID_group
      0        ord  google                1
      0        ord  google                2
      0        ord  google                3
      0        ord  google                4
      0        ord  google                5
      0        ord  google                6
      1     inflow  appier                0
      

      如果Contains ID_group包含字符串,则必须先eval

      >>> type(graph.at[0, 'Content ID_group'])
      str
      
      >>> graph['Content ID_group'] = graph['Content ID_group'].apply(pd.eval)
      
      >>> graph.explode('Content ID_group')
        Event Name   Media Content ID_group
      0        ord  google                1
      0        ord  google                2
      0        ord  google                3
      0        ord  google                4
      0        ord  google                5
      0        ord  google                6
      1     inflow  appier                0
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-12-24
        • 1970-01-01
        • 1970-01-01
        • 2014-04-27
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多