簡體   English   中英

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

[英]How to compare strings in a list using python?

這是我的數據幀:

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]時它不起作用

這是我的解決方案。 您可以使用以下命令創建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同時獲取indeces和值,從dataframe_1變量開始。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM