pandas.concat:无法处理非唯一的多索引!熊猫蟒
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31981443/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
pandas.concat: Cannot handle a non-unique multi-index! Pandas Python
提问by user1129988
I am trying to concatenate 100 dataframes that have 2 datetime indexes using the following code:
我正在尝试使用以下代码连接具有 2 个日期时间索引的 100 个数据帧:
concat_df = pd.concat([df_dict[c] for c in df_dict], axis = 1)
But somewhere one of the dataframes (I assume it is one but it could be more) is causing the following exception to occur:
但是某个数据帧之一(我假设它是一个,但可能更多)导致以下异常发生:
Exception: cannot handle a non-unique multi-index!
Any ideas why?
任何想法为什么?
Is it referring to the first index or the second index?
它是指第一个索引还是第二个索引?
回答by user1129988
I found it was referring to the first index my solution was: (thought not sure how efficient it is but the concat works afterwards)
我发现它指的是我的解决方案的第一个索引:(我不确定它的效率如何,但之后 concat 可以工作)
dup_first_index_dates = np.where(np.array([np.sum(df_dict[c].index.duplicated()) for c in df_dict]) == 1)[0]
key = df_dict.keys()
for i in dup_first_index_dates :
df_dict[key[i]] = df_dict[tickers[i]].reset_index().drop_duplicates('Level1').set_index(['Level1', 'Level2'])
回答by giulio
probably the following option ignore_index=Trueof concathelps.
可能是以下选项ignore_index=True的concat帮助。
not sure if this could be use like follows:
不确定这是否可以使用如下:
concat_df = pd.concat([df_dict[c] for c in df_dict], axis = 1,ignore_index=True)
to try!
试试吧!

