Pandas concat yields ValueError: Plan shapes are not aligned

后端 未结 6 721
忘掉有多难
忘掉有多难 2020-12-03 02:23

In pandas, I am attempting to concatenate a set of dataframes and I am getting this error:

ValueError: Plan shapes are not aligned

My underst

6条回答
  •  执念已碎
    2020-12-03 02:56

    I recently got this message, too, and I found like user @jason and @user3805082 above that I had duplicate columns in several of the hundreds of dataframes I was trying to concat, each with dozens of enigmatic varnames. Manually searching for duplicates was not practical.

    In case anyone else has the same problem, I wrote the following function which might help out.

    def duplicated_varnames(df):
        """Return a dict of all variable names that 
        are duplicated in a given dataframe."""
        repeat_dict = {}
        var_list = list(df) # list of varnames as strings
        for varname in var_list:
            # make a list of all instances of that varname
            test_list = [v for v in var_list if v == varname] 
            # if more than one instance, report duplications in repeat_dict
            if len(test_list) > 1: 
                repeat_dict[varname] = len(test_list)
        return repeat_dict
    

    Then you can iterate over that dict to report how many duplicates there are, delete the duplicated variables, or rename them in some systematic way.

提交回复
热议问题