Home > Enterprise >  How do I find the count of a column of lists and display by date?
How do I find the count of a column of lists and display by date?

Time:07-22

My dataset looks like this enter image description here

Using python and pandas I want to display the count of each unique item in the coverage column which are stored in a list shown in the table.

I want to display that count by device and by date.

Example out put would be:

enter image description here

the unique coverage count being the count of each unique list value in the "coverage" row

CodePudding user response:

You can use apply method to iterate over rows and apply a custom function. This function may return the length of the list. For example:

df["covarage_count"] = df["coverage"].apply(lambda x: len(x))

CodePudding user response:

Here's how I solved it using for loops

coverage_list = []
for item in list(df["coverage"]):
  if item == '[]':
    item = ''
  else:
    item = list(item.split(","))

  coverage_list.append(len(item))
  # print(len(item))


df["coverage_count"] = coverage_list  
  • Related