对于数据框:
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的情况下执行此操作?
江户川乱折腾
相关分类