首页 > 解决方案 > 将多个返回值分成单独的行的python语法

问题描述

我正在使用谷歌搜索方法来获得五只动物的前 5 个链接,我想为每只动物制作一个数据框(每只动物有五个链接)。一种动物(熊猫)的数据框基本上需要如下所示。有五行,col 1是panda,col 2是ONE link

在此处输入图像描述

但是现在它看起来像这样(如下所示)只有一行,col 1 panda,col 2 所有五个链接都在一个单元格中

在此处输入图像描述

我如何做到这一点,以便我的代码将创建一个数据框,将五个链接分成单独的行中的单元格,如图像 #1 中所示?有python语法吗?(我想通过 for 循环运行代码,但我得到一个 AttributeError。代码应该适用于为每个动物创建单独数据框的动物列表,Panda 只是动物数据框应该是什么样子的一个示例) .

标签: pythonpandasdataframedata-science

解决方案


为每个动物创建新的 DF

您可以拆分和分解数据框。然后使用 groupby 为每个 Animal 创建一个单独的数据框。这是如何做到的。

import pandas as pd
df = pd.DataFrame({'Animal':['Panda', 'Tiger','Monkey'],
                   'Link':['abcde.com, fghijk.com, lmnopq.com, rstuvw.com, xyz.com',
                           'adobe.com, facebook.com, linkedin.com, google.com, citi.com',
                           'amazon.com, bbc.com, cnn.com, fox.com, abc.com'],})

#Convert all the data into multiple rows
df = (df.set_index(['Animal'])
   .apply(lambda x: x.str.split(',').explode())
   .reset_index()) 

#create a dictionary of pandas dataframe for each animal
d = dict(tuple(df.groupby('Animal')))

#store the dataframes into a list
dfx = []

#Iterate through each key in the dictonary, and append to list
for k in d:
    dfx.append(d[k])

#example
print (type(dfx[1])) #will result in <class 'pandas.core.frame.DataFrame'>

print (dfx[0]) #will print dataframe for Animal = 'Monkey'

print (dfx[1]) #will print dataframe for Animal = 'Panda'

print (dfx[2]) #will print dataframe for Animal = 'Tiger'

其输出将是:

列表中每个 DataFrame 的类型dfx为:

<class 'pandas.core.frame.DataFrame'>

dfx[0]会给你:

    Animal        Link
10  Monkey  amazon.com
11  Monkey     bbc.com
12  Monkey     cnn.com
13  Monkey     fox.com
14  Monkey     abc.com

dfx[1]会给你:

  Animal         Link
0  Panda    abcde.com
1  Panda   fghijk.com
2  Panda   lmnopq.com
3  Panda   rstuvw.com
4  Panda      xyz.com

dfx[2]会给你:

  Animal           Link
5  Tiger      adobe.com
6  Tiger   facebook.com
7  Tiger   linkedin.com
8  Tiger     google.com
9  Tiger       citi.com

请注意,groupby 将使用字母顺序,因此 Monkey,然后是 Panda,然后是 Tiger

上一个拆分和爆炸的解决方案

我将这样做。

import pandas as pd
df = pd.DataFrame({'Animal':['Panda'],
                   'Link':['abcde.com, fghijk.com, lmnopq.com, rstuvw.com, xyz.com']})
print (df)
df = (df.set_index(['Animal'])
   .apply(lambda x: x.str.split(',').explode())
   .reset_index()) 
print (df)

原始数据框:

  Animal                                                    Link
0  Panda  abcde.com, fghijk.com, lmnopq.com, rstuvw.com, xyz.com

更新的数据框:

  Animal         Link
0  Panda    abcde.com
1  Panda   fghijk.com
2  Panda   lmnopq.com
3  Panda   rstuvw.com
4  Panda      xyz.com

我没有更改任何代码。这是具有多条记录的解决方案。

import pandas as pd
df = pd.DataFrame({'Animal':['Panda', 'Tiger','Monkey'],
                   'Link':['abcde.com, fghijk.com, lmnopq.com, rstuvw.com, xyz.com',
                           'adobe.com, facebook.com, linkedin.com, google.com, citi.com',
                           'amazon.com, bbc.com, cnn.com, fox.com, abc.com'],})
print (df)
df = (df.set_index(['Animal'])
   .apply(lambda x: x.str.split(',').explode())
   .reset_index()) 
print (df)

前:

Animal                                                            Link
0   Panda        abcde.com, fghijk.com, lmnopq.com, rstuvw.com,xyz.com
1   Tiger  adobe.com, facebook.com, linkedin.com, google.com, citi.com
2  Monkey               amazon.com, bbc.com, cnn.com, fox.com, abc.com

后:

    Animal           Link
0    Panda      abcde.com
1    Panda     fghijk.com
2    Panda     lmnopq.com
3    Panda     rstuvw.com
4    Panda        xyz.com
5    Tiger      adobe.com
6    Tiger   facebook.com
7    Tiger   linkedin.com
8    Tiger     google.com
9    Tiger       citi.com
10  Monkey     amazon.com
11  Monkey        bbc.com
12  Monkey        cnn.com
13  Monkey        fox.com
14  Monkey        abc.com

推荐阅读