This question already has answers here:
pandas - combine strings of row
                                
                                    (2个答案)
                                
                        
                                去年关闭。
            
                    
我在下面的数据框nbr

||Postal_Code|Borough|Neighborhood|
|0|M3A|North York|Parkwoods|
|1|M4A|North York|Victoria Village|
|2|M5A|Downtown Toronto|Harbourfront|
|3|M5A|Downtown Toronto|Regent Park|
|4|M6A|North York|Lawrence Heights|
|5|M6A|North York|Lawrence Manor|
|6|M7A|Queen’s Park|Queen’s Park|


我想运行Python代码,使第4行和第5行应合并为1行,并返回如下结果:(我尝试过groupbyagg方法,但它们在这里不起作用)

||Postal_Code|Borough|Neighborhood|
|0|M3A|North York|Parkwoods|
|1|M4A|North York|Victoria Village|
|2|M5A|Downtown Toronto|Harbourfront|
|3|M5A|Downtown Toronto|Regent Park|
|4|M6A|North York|Lawrence Heights , Lawrence Manor|
|5|M7A|Queen’s Park|Queen’s Park|


代码如下:

nbr1.index = pd.RangeIndex(len(nbr1.index))
More than one neighborhood can exist in one postal code area.

for row_index,row in nbr1.iterrows():
    if(nbr1.loc[row_index,[‘Postal_Code’]].values.astype(‘str’) == nbr1.loc[row_index + 1,[‘Postal_Code’]].values.astype(‘str’)):
        print(‘inside same Postal code’)
        print(nbr1.loc[row_index,[‘Postal_Code’]].values.astype(‘str’))
        print(nbr1.loc[row_index + 1,[‘Postal_Code’]].values.astype(‘str’))

    if(nbr1.loc[row_index,['Borough']].values.astype('str') == nbr1.loc[row_index + 1,['Borough']].values.astype('str')):
        print('inside same Borough')
        print(nbr1.loc[row_index,['Borough']].values.astype('str'))
        print(nbr1.loc[row_index + 1,['Borough']].values.astype('str'))
        print(nbr1.loc[row_index,['Neighborhood']].values.astype('str'))
        print(nbr1.loc[row_index + 1,['Neighborhood']].values.astype('str'))
        print('Adding')
        nbr1[row_index,['Neighborhood']] = nbr1.loc[row_index,['Neighbourhood']].values.astype('str').apply(lambda x: '-'.join(x +1), axis=1)

最佳答案

您可以使用groupbyagg

df.groupby('Postal_Code').agg({'Borough':'first',
                               'Neighborhood': ', '.join}).reset_index()


输出:

  Postal_Code   Borough            Neighborhood
0   M3A         North York          Parkwoods
1   M4A         North York         Victoria Village
2   M5A       Downtown Toronto   Harbourfront, Regent Park
3   M6A         North York       Lawrence Heights, Lawrence Manor
4   M7A         Queen’s Park        Queen’s Park

关于python - 在 Pandas 中将相似的行合并为单行,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52466374/

10-11 07:36