【发布时间】:2020-05-25 17:23:44
【问题描述】:
我有一列应该只包含整数,但是由于数据错误,它当前包含字符串和整数。我需要应用一个np.where 声明,上面写着np.where(df['IO8'] >= 2002),"NEW","OLD")
语句失败并出现错误 cannot use >= on strings。我将如何解决这个问题?任何帮助都会很棒。让我知道是否需要更多细节。我也尝试过像下面这样使用正则表达式:
df['split'] = pd.np.where(df['IO8'].str.contains("^\d{4}$", regex=True), "Number", "Error")
df['IO8'] = pd.np.where(df['split'].str.contains("Number"), df['IO8'].astype(int), df['IO8'].astype(str))
df['split1'] = pd.np.where(df['split'].str.contains("Number") & (df['IO8'] >= 2002),"NEW","OLD")
但仍然会出现错误。
【问题讨论】:
-
只是为了确保@Jezrael 回答的工作,你能
print(df['IO8'].head())吗?
标签: python regex python-3.x pandas numpy