Как найти количество строк во фрейме данных, которые почти повторяются, то есть отличаются менее чем на две записи? - PullRequest
0 голосов
/ 11 декабря 2018

У меня есть pandas dataframe, который выглядит примерно так:

     | col1 | col2 | col3 | col4 | col5 | col6 | col7
row1 |  a   |  b   |  c   |  d   |  e   |  f   |  g
row2 |  a   |  a   |  c   |  d   |  e   |  f   |  g   
row3 |  a   |  b   |  c   |  d   |  a   |  a   |  g   
row4 |  a   |  q   |  q   |  q   |  q   |  q   |  q  

Я хотел бы рассчитать количество строк, совпадающих с другой строкой, за исключением менее двух записей, и поместить их вколонка / серия.

Таким образом, в этом случае строки 2 и 3 похожи на 1. Поэтому запись для строки 1 будет 2. Общий результат будет:

     | col1 | col2 | col3 | col4 | col5 | col6 | col7  | almost_dups
row1 |  a   |  b   |  c   |  d   |  e   |  f   |  g    |  2
row2 |  a   |  a   |  c   |  d   |  e   |  f   |  g    |  1
row3 |  a   |  b   |  c   |  d   |  e   |  a   |  a    |  1 
row4 |  a   |  q   |  q   |  q   |  q   |  q   |  q    |  0

Моя первоначальная мысль состоит в том, чтобы определитьметрика расстояния между строками.

Ответы [ 2 ]

0 голосов
/ 11 декабря 2018

это сработает (хотя не уверен, что оптимизировано)

cols = df.columns
df.reset_index(inplace=True)
df_result = pd.DataFrame()
df_result['index'] = df['index']
df_result['result'] = 0
for index_ln, row_ln in df.iterrows():
    count_same = 0
    for index_col, row_col in df.iterrows():        
        count=0
        for col in cols:
            if row_ln[col] != row_col[col]:
                count+=1
        if count<=2:
            count_same+=1        
    df_result['result'] = np.where(df_result['index']==row_ln['index'], count_same-1, df_result['result'])
    print(count_same)
df = df.merge(df_result, on='index')
0 голосов
/ 11 декабря 2018

Как насчет этого кода.Быстрое решение для новичка здесь, но я думаю, что это работает хорошо.

import pandas as pd
# let's create the dataframe
df = pd.DataFrame(data = {'col1': ['a','a','a','a'], 
                          'col2': ['b','a','b','q'],
                          'col3': ['c','c','c','q'],
                          'col4': ['d','d','d','q'], 
                          'col5': ['e','e','a','q'],
                          'col6': ['f','f','a','q'],
                          'col7': ['g','g','g','q']} )

almost_dups = []            # initialize the list we want to compute    
for i in range(len(df)):    # for every dataframe row
    a = df.iloc[i].values   # get row values
    count = 0               # this will count the rows similar to the selected one 
    for j in range(len(df)): # for every other row
        if i!=j:            # if rows are different
            b = df.iloc[j].values
            if sum([i == j for i, j in zip(a, b)])>= 5: # if at least 5 values are same
                count +=1   # increase counter
    almost_dups.append(count) # append the count
df['almost_dups'] = almost_dups   # append the list to dataframe, as a new column
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...