Search code examples
pythonpandasdataframedictionarydataset

how to create a dictionary from a dataframe where the keys are column names and values are the number of values under each column?


I have a data frame whose dimensions are (356027, 163). I want to create a dictionary from the data frame where i will have 163 keys with values which are the number of entries in them(i.e. number of non null entries) I tried using the to_dict() operation but couldn't insert the values as the count of entries under each column

dict1 = data-frame.to_dict('index')
print(dict1)
for i in dict1:
    print(i)

Solution

  • Use DataFrame.count with Series.to_dict, loop by dict is not necessary:

    out = data-frame.count().to_dict()