我已将连续特征转换为分类特征。我在 Pandas 中得到 NaN

我已将连续数据集转换为分类数据集。当转换后连续数据的值为 0.0 时,我会得到 nan 值。下面是我的代码


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)

category = category.to_frame()

print (category)

如何转换这些值,以免我得到 NaN 值。我附上了两个屏幕截图,以便更好地了解实际数据的外观以及转换数据的外观。这是主要数据集。这是使用 bins 和 pandas.cut() 后的结果。“0.00”如何与数据集中的其他值保持一致。


湖上湖
浏览 117回答 1
1回答

白板的微信

使用时pd.cut,可以指定参数include_lowest = True。这将使第一个内部左包含(它将包括 0 值,因为您的第一个间隔以 0 开头)。因此,在您的情况下,您可以将代码调整为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,include_lowest=True)category = category.to_frame()print (category)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python