如何使用python比较列表中的字符串?

时间:2019-06-14 04:32:16

标签: python pandas list dataframe

这是我的数据框:

CommitId    RefactoringType Detail
0   d38f7b334856ed4007fb3ec0f8a5f7499ee2f2b8    Pull Up Attribute   blokusgame.mi.android.hazi.blokus.GameLogic.Pl...
1   d38f7b334856ed4007fb3ec0f8a5f7499ee2f2b8    Pull Up Attribute   blokusgame.mi.android.hazi.blokus.GameLogic.Pl...
2   d38f7b334856ed4007fb3ec0f8a5f7499ee2f2b8    Pull Up Attribute   blokusgame.mi.android.hazi.blokus.GameLogic.Pl...
3   4bb968a47ce00279d6051df95bd782650700179e    Extract Method  blokusgame.mi.android.hazi.blokus.GameLogic.Pl...
4   c3d7ec38417ecff03d1cd3be0163e6ce07578eb3    Extract Method  blokusgame.mi.android.hazi.blokus.GameLogic.Pl...
5   00568c9886e739d6b5dd61b4a4326d598552fb6f    Extract Method  blokusgame.mi.android.hazi.blokus.GameLogic.Block
6   00568c9886e739d6b5dd61b4a4326d598552fb6f    Extract Method  blokusgame.mi.android.hazi.blokus.GameLogic.Block
7   00568c9886e739d6b5dd61b4a4326d598552fb6f    Extract Method  blokusgame.mi.android.hazi.blokus.GameLogic.Block
8   00568c9886e739d6b5dd61b4a4326d598552fb6f    Extract Method  blokusgame.mi.android.hazi.blokus.GameLogic.Block

我阅读了CommitId列并将其放入列表中。现在我需要比较CommitId值(如果它们相等),我提取所有行并将其放入其他数据帧中,所以这是我需要的输出:

dataframe1:


0   d38f7b334856ed4007fb3ec0f8a5f7499ee2f2b8    Pull Up Attribute   blokusgame.mi.android.hazi.blokus.GameLogic.Pl...
1   d38f7b334856ed4007fb3ec0f8a5f7499ee2f2b8    Pull Up Attribute   blokusgame.mi.android.hazi.blokus.GameLogic.Pl...
2   d38f7b334856ed4007fb3ec0f8a5f7499ee2f2b8    Pull Up Attribute   blokusgame.mi.android.hazi.blokus.GameLogic.Pl..

dataframe2:


3   4bb968a47ce00279d6051df95bd782650700179e    Extract Method  blokusgame.mi.android.hazi.blokus.GameLogic.Pl...

dataframe3:

4   c3d7ec38417ecff03d1cd3be0163e6ce07578eb3    Extract Method  blokusgame.mi.android.hazi.blokus.GameLogic.Pl...

dataframe4:

5   00568c9886e739d6b5dd61b4a4326d598552fb6f    Extract Method  blokusgame.mi.android.hazi.blokus.GameLogic.Block
6   00568c9886e739d6b5dd61b4a4326d598552fb6f    Extract Method  blokusgame.mi.android.hazi.blokus.GameLogic.Block
7   00568c9886e739d6b5dd61b4a4326d598552fb6f    Extract Method  blokusgame.mi.android.hazi.blokus.GameLogic.Block
8   00568c9886e739d6b5dd61b4a4326d598552fb6f    Extract Method  blokusgame.mi.android.hazi.blokus.GameLogic.Block

这是我的代码:

list=[]
for elm in df['CommitId']:
   list.append(elm)
   print(list)

length = len(list) 
for i in range(length):
    if()

问题出在if()中,我没有找到一种方法来比较CommitId并提取所有行。当我尝试比较list [i]和list [i + 1]时不起作用

1 个答案:

答案 0 :(得分:0)

这是我的解决方案。 您可以使用以下命令创建DataFrameGroupBy可迭代对象:

group = your_dataframe.groupby(['CommitId'])

将CommitId列中的相等值分组,然后可以使用exec命令在for循环中创建多个数据帧:

for index,values in enumerate(group):
    exec(f'dataframe_{index+1} = pd.DataFrame(values[1])')

我使用enumerate同时从groupby迭代器对象获取索引和值,并使用index+1从dataframe_1变量开始获取