处理ValueError的简便方法:无法从重复的轴重新索引 [英] Convenient way to deal with ValueError: cannot reindex from a duplicate axis
问题描述
我能够搜索显示此错误消息原因"的建议,但无法解决该问题-
I am able to search suggestions that show the 'cause' of this error message, but not how to address it -
每次我尝试通过在2个现有列中串联字符串值来向pandas数据帧添加新列时遇到此问题.
I encounter this problem every time I try to add a new column to a pandas dataframe by concatenating string values in 2 existing columns.
例如:
wind['timestamp'] = wind['DATE (MM/DD/YYYY)'] + ' ' + temp['stamp']
如果第一个项目和第二个项目与''合并为单独的数据帧/系列,则可以使用
It works if the first item and the second merged with ' ' are each separate dataframe/series.
这些尝试是为了使日期&时间合并到同一列中,以便熊猫图书馆将它们识别为日期时间戳.
These attempts are to have date & time merged into the same column so that they get recognized as datetime stamps by pandas library.
我不确定我是错误使用命令还是熊猫库功能在内部受到限制,因为它会不断返回 duplicate axis
错误msg.我知道后者极不可能哈哈哈...
I am not certain if I am wrongly using the command or if it is the pandas library features are internally limited, as it keeps returning the duplicate axis
error msg. I understand the latter is highly unlikely hahaha ...
我能听到一些快速简便的解决方案吗?
Could I hear some quick and easy solution out of this?
我的意思是,我认为数据帧中列值之间的加/减以及所有这些运算将非常容易.在表上也可以看到它应该不难吗?
I mean, I thought sum/subtract and all these operations between column values in a dataframe would be quite easy. Shouldn't be too hard to have it visible on the table either right?
推荐答案
系列之间的操作需要无重复的索引,否则Pandas不知道如何在计算中对齐值.当前您的数据不是这种情况.
Operations between series require non-duplicated indices, otherwise Pandas doesn't know how to align values in calculations. This isn't the case with your data currently.
如果确定序列按位置对齐 ,则可以在每个数据帧上调用 reset_index
:
If you are certain that your series are aligned by position, you can call reset_index
on each dataframe:
wind = pd.DataFrame({'DATE (MM/DD/YYYY)': ['2018-01-01', '2018-02-01', '2018-03-01']})
temp = pd.DataFrame({'stamp': ['1', '2', '3']}, index=[0, 1, 1])
# ATTEMPT 1: FAIL
wind['timestamp'] = wind['DATE (MM/DD/YYYY)'] + ' ' + temp['stamp']
# ValueError: cannot reindex from a duplicate axis
# ATTEMPT 2: SUCCESS
wind = wind.reset_index(drop=True)
temp = temp.reset_index(drop=True)
wind['timestamp'] = wind['DATE (MM/DD/YYYY)'] + ' ' + temp['stamp']
print(wind)
DATE (MM/DD/YYYY) timestamp
0 2018-01-01 2018-01-01 1
1 2018-02-01 2018-02-01 2
2 2018-03-01 2018-03-01 3
这篇关于处理ValueError的简便方法:无法从重复的轴重新索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!