I'm getting the following error message: ValueError: cannot reindex from a duplicate axis

I'm getting the following error message: ValueError: cannot reindex from a duplicate axis

为什么我会收到此错误消息?我的代码:

my_df.loc[my_df['col1'] < my_df['col2'],'col3'] = my_df['col1'].

基本上我想做的是在 col3 小于 col2 时将 col3 设置为 col1。谢谢!

您似乎在尝试插入不同的数据大小。列 col3 的大小必须与 col1 的大小相同。
试试这个:

my_df.loc[my_df['col1'] < my_df['col2'],'col3'] = my_df.loc[my_df['col1'] < my_df['col2'],'col1']

我通过使用 reset_index() 重置数据帧上的索引解决了这个问题。由于先前的 concat 函数,索引得到 "messed up"。