There is a pandas of the form:

Seen Buyed 0 0,1,2,3,4,5 NaN 1 0, 2 NaN ........ 

Column - Seen lines, which is actually a list of numbers. It is necessary to turn this column into one general list of all numbers:

 [0,1,2,3,4,5,0,2...] 

And then make a frequency dictionary)))

  • 3
    In Russian, stackoverflow is used to ask questions in Russian. Translate please. - Sergey Glazirin
  • Excuse me. There is a Pandas view above. Column - Seen lines, which are actually a list of numbers. It is necessary to turn this column into one general list of all numbers. And then make a frequency dictionary))) - Alex Ma
  • The question has a button to править , click it, and translate the question there, because I’ll most likely close it for non-compliance with the site rules. - Sergey Glazirin

2 answers 2

You can glue the rows in the Pandas DataFrame column using the Series.str.cat () method:

 In [294]: s = df['Seen'].str.cat(sep=',') In [295]: s Out[295]: '0,1,2,3,4,5,0, 2' 

You can split the CSV lines into lists:

 In [304]: df['Seen'].str.split(',\s*') Out[304]: 0 [0, 1, 2, 3, 4, 5] 1 [0, 2] Name: Seen, dtype: object In [305]: df['Seen'].str.split(',\s*').sum() Out[305]: ['0', '1', '2', '3', '4', '5', '0', '2'] 

Frequency dictionary:

 In [311]: df['Seen'].str.split(',\s*').apply(pd.Series).stack().astype(int).value_counts() Out[311]: 2 2 0 2 5 1 4 1 3 1 1 1 dtype: int64 

without conversion to numbers (int):

 In [312]: df['Seen'].str.split(',\s*').apply(pd.Series).stack().value_counts() Out[312]: 2 2 0 2 3 1 4 1 1 1 5 1 dtype: int64 In [313]: df['Seen'].str.split(',\s*').apply(pd.Series).stack().value_counts().to_dict() Out[313]: {'0': 2, '1': 1, '2': 2, '3': 1, '4': 1, '5': 1} 

    To immediately make a frequency dictionary:

     >>> from collections import Counter >>> df['Seen'].str.split(',').map(lambda lst: Counter(map(int, lst))).sum() Counter({0: 2, 2: 2, 1: 1, 3: 1, 4: 1, 5: 1}) 

    If an intermediate list is needed:

     >>> [n for lst in df['Seen'].str.split(',') for n in map(int, lst)] [0, 1, 2, 3, 4, 5, 0, 2]