import pandas as pd
import numpy as np
df = pd.Series([1, 2, 3, 4], index=['a', 'b', 'c', np.nan])
df.pop(np.nan)
Type error : can not do label indexing on class pandas.core.indexes.base.Index with these index [nan] of class float
我试过了
df.reset_index().dropna().set_index('index')
但是当我这样做时,df.pop('a')它给了我错误。
解决办法:
如果s是pandas Series,则s.reset_index()返回一个DataFrame,其中Series的索引作为其列之一(index默认情况下命名)。请注意,s.reset_index(drop=True)返回Series,但会丢弃索引。
我的任务的一个解决方案是选择0由您的最后一行构建的DataFrame 命名的唯一列:
# setup with the name "s" to represent a Series (keep "df" for DataFrames)
s = pd.Series([1,2,3,4], index=['a','b','c',np.nan])
res1 = s.reset_index().dropna().set_index('index')[0]
res1
index
a 1
b 2
c 3
Name: 0, dtype: int64
另一种选择是通过重新索引系列来删除空索引标签:
res2 = s.loc[s.index.dropna()]
res2
a 1
b 2
c 3
dtype: int64








暂无数据