【问题标题】:Create a dataframe with columns and their unique values in pandas在 pandas 中创建包含列及其唯一值的数据框
【发布时间】:2022-11-11 05:02:32
【问题描述】:

我试图寻找一种方法来创建列及其唯一值的数据框。我知道这用例较少,但将是初步了解唯一值的好方法。它看起来像这样......

State County City
Colorado Denver Denver
Colorado El Paso Colorado Springs
Colorado Larimar Fort Collins

变成了这个... |状态 |县 |城市 | | -------- | -------------- |------| |科罗拉多 |丹佛 |丹佛 | | |埃尔帕索 |科罗拉多斯普林斯 | | |拉里马尔 |柯林斯堡 | | | |拉夫兰 |

【问题讨论】:

    标签: python pandas unique


    【解决方案1】:

    这是我想出的最好的解决方案,希望能帮助其他人寻找类似的东西!

    def create_unique_df(df) -> pd.DataFrame:
        """ take a dataframe and creates a new one containing unique values for each column
        note, it only works for two columns or more
    
        :param df: dataframe you want see unique values for
        :param type: pandas.DataFrame
        return: dataframe of columns with unique values
        """
        # using list() allows us to combine lists down the line
        data_series = df.apply(lambda x: list( x.unique() ) )
    
        list_df = data_series.to_frame()
    
        # to create a df from lists they all neet to be the same leng. so we can append null 
        # values
        # to lists and make them the same length. First find differenc in length of longest list and
        # the rest
        list_df['needed_nulls'] = list_df[0].str.len().max() - list_df[0].str.len()
    
        # Second create a column of lists with one None value
        list_df['null_list_placeholder'] = [[None] for _ in range(list_df.shape[0])]
    
        # Third multiply the null list times the difference to get a list we can add to the list of
        # unique values making all the lists the same length. Example: [None] * 3  == [None, None, 
        # None]
        list_df['null_list_needed'] = list_df.null_list_placeholder * list_df.needed_nulls
        list_df['full_list'] = list_df[0] + list_df.null_list_needed
    
        unique_df = pd.DataFrame(
            list_df['full_list'].to_dict()
        )
    
        return unique_df
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-03-04
      • 1970-01-01
      • 2020-02-20
      • 1970-01-01
      • 2021-11-10
      • 1970-01-01
      • 2020-09-10
      • 1970-01-01
      相关资源
      最近更新 更多