我已将连续数据集转换为分类数据集。当转换后连续数据的值为 0.0 时,我会得到 nan 值。下面是我的代码import pandas as pdimport matplotlib as pltdf = pd.read_csv('NSL-KDD/KDDTrain+.txt',header=None)data = df[33]bins = [0.000,0.05,0.10,0.15,0.20,0.25,0.30,0.35,0.40,0.45,0.50,0.55,0.60,0.65,0.70,0.75,0.80,0.85,0.90,0.95,1.00]category = pd.cut(data,bins)category = category.to_frame()print (category)如何转换这些值,以免我得到 NaN 值。我附上了两个屏幕截图,以便更好地了解实际数据的外观以及转换数据的外观。这是主要数据集。这是使用 bins 和 pandas.cut() 后的结果。“0.00”如何与数据集中的其他值保持一致。
1 回答
白板的微信
TA贡献1883条经验 获得超3个赞
使用时pd.cut,可以指定参数include_lowest = True。这将使第一个内部左包含(它将包括 0 值,因为您的第一个间隔以 0 开头)。
因此,在您的情况下,您可以将代码调整为
import pandas as pd
import matplotlib as plt
df = pd.read_csv('NSL-KDD/KDDTrain+.txt',header=None)
data = df[33]
bins = [0.000,0.05,0.10,0.15,0.20,0.25,0.30,0.35,0.40,0.45,0.50,0.55,0.60,0.65,0.70,0.75,0.80,0.85,0.90,0.95,1.00]
category = pd.cut(data,bins,include_lowest=True)
category = category.to_frame()
print (category)
添加回答
举报
0/150
提交
取消