对于数据帧:
df = pd.DataFrame({
'key': [1,2,3,4,5, np.nan, np.nan],
'value': ['one','two','three', 'four', 'five', 'six', 'seven']
}).set_index('key')
看起来是这样的:
value
key
1.0 one
2.0 two
3.0 three
4.0 four
5.0 five
NaN six
NaN seven
我想把它分为:
value
key
1 one
1 one
6 NaN
这将产生一个警告:
df.loc[[1,1,6],]
Passing list-likes to .loc or [] with any missing label will raise
KeyError in the future, you can use .reindex() as an alternative.
这会产生一个错误:
df.reindex([1, 1, 6])
ValueError: cannot reindex from a duplicate axis
如何在引用缺少的索引而不使用apply时执行此操作?你知道吗
问题是有重复的值
NaN
作为索引。在重新编制索引时,您应该取消这些索引,因为它们是重复的,并且在新索引中使用哪个值存在歧义。你知道吗对于广义解,使用
duplicated
如果要保留重复的索引并使用
reindex
,则会失败。这has actually been asked before几次相关问题 更多 >
编程相关推荐