import pandas as pd

def count_data_info(df):
    data_info = []
    for column in df.columns:
        data_info.append((column, df[column].isnull().sum(), df[column].isnull().sum()/df[column].isnull().count() * 100, 
                          df[column].nunique(), df[column].value_counts(normalize = True).values[0] * 100))
    data_info_df = pd.DataFrame(data_info, columns = ['column name','total null values', 'percent of null values',
                                                     'unique values','percent of most popular values'])
    return data_info_df

def make_df_by_data_info(df, data_info_df, by_column, threshold, more = True):
    test_columns = (data_info_df[data_info_df[by_column] > threshold]['column name'].tolist() if more else
    data_info_df[data_info_df[by_column] <= threshold]['column name'].tolist())
    test_columns.append('HasDetections')
    test_columns = set(test_columns)
    test_columns.intersection_update(df.columns)
    return df[test_columns]