Pandas concat flips all my values in the DataFrame

后端 未结 3 1278
北恋
北恋 2021-01-29 06:31

I have a dataframe called \'running_tally\'

        list   jan_to  jan_from
0         LA    True      False
1         NY   False       True

I am

相关标签:
3条回答
  • 2021-01-29 06:49

    I am not sure why you want to use a groupby in this case... when using concat there is no need to specify which columns you want to use, as long as their names are identical. Simple concatenation like this should do:

    running_tally = pd.concat([running_tally,new_data], ignore_index=True, sort=False)
    

    EDIT to take question edit into account: this should do the same job, without duplicates.

    running_tally = running_tally.merge(new_data, on="list", how="outer")
    
    0 讨论(0)
  • 2021-01-29 06:56

    I don´t get the booleans flipped as you, but you can try this too:

    running_tally=running_tally.append(new_data,ignore_index=True)
    print(running_tally)
    

    Output:

      list jan_to jan_from
    0   LA   True    False
    1   NY  False     True
    2  HOU    NaN      NaN
    

    EDIT: Since the question was edited, you could try with:

    running_tally=running_tally.append(new_data,ignore_index=True).groupby('list',as_index=False).first()
    
    0 讨论(0)
  • 2021-01-29 06:58

    The actual row order was being flipped when using concat for pandas 0.20.1

    How to concat pandas Dataframes without changing the column order in Pandas 0.20.1?

    0 讨论(0)
提交回复
热议问题