gpt4 book ai didi

python - 如何替换 Pandas 中的值?

转载 作者:行者123 更新时间:2023-12-03 16:59:02 25 4
gpt4 key购买 nike

试图在 "KDDTest+.csv" 的倒数第二列中对 23 个不同的标签进行分组分成四组。请注意,在执行此操作之前,我已删除了 csv 的最后一列。
我已经阅读了 .csv 文件使用

df = pd.read_csv('KDDTrain+.csv', header=None, names = col_names)
在哪里
col_names = ["duration","protocol_type","service","flag","src_bytes",
"dst_bytes","land","wrong_fragment","urgent","hot","num_failed_logins",
"logged_in","num_compromised","root_shell","su_attempted","num_root",
"num_file_creations","num_shells","num_access_files","num_outbound_cmds",
"is_host_login","is_guest_login","count","srv_count","serror_rate",
"srv_serror_rate","rerror_rate","srv_rerror_rate","same_srv_rate",
"diff_srv_rate","srv_diff_host_rate","dst_host_count","dst_host_srv_count",
"dst_host_same_srv_rate","dst_host_diff_srv_rate","dst_host_same_src_port_rate",
"dst_host_srv_diff_host_rate","dst_host_serror_rate","dst_host_srv_serror_rate",
"dst_host_rerror_rate","dst_host_srv_rerror_rate","label"]
如果我打印出数据框的前 5 行,这是输出(请注意“标签”列):
使用 print(df.head(5))
   duration protocol_type  ... dst_host_srv_rerror_rate    label
0 0 tcp ... 0.00 normal
1 0 udp ... 0.00 normal
2 0 tcp ... 0.00 neptune
3 0 tcp ... 0.01 normal
4 0 tcp ... 0.00 normal
我已经根据我在网上找到的内容尝试了这两种分组方法:
方法一:
df.replace(to_replace = ['ipsweep.', 'portsweep.', 'nmap.', 'satan.'], value = 'probe', inplace = True)
df.replace(to_replace = ['ftp_write.', 'guess_passwd.', 'imap.', 'multihop.', 'phf.', 'spy.', 'warezclient.', 'warezmaster.'], value = 'r2l', inplace = True)
df.replace(to_replace = ['buffer_overflow.', 'loadmodule.', 'perl.', 'rootkit.'], value = 'u2r', inplace = True)
df.replace(to_replace = ['back.', 'land.' , 'neptune.', 'pod.', 'smurf.', 'teardrop.'], value = 'dos', inplace = True)
方法二:
df['label'] = df['label'].replace(['ipsweep.', 'portsweep.', 'nmap.', 'satan.'], 'probe',regex=True)
df['label'] = df['label'].replace(['ftp_write.', 'guess_passwd.', 'imap.', 'multihop.', 'phf.', 'spy.', 'warezclient.', 'warezmaster.'], 'r2l',regex=True)
df['label'] = df['label'].replace(['buffer_overflow.', 'loadmodule.', 'perl.', 'rootkit.'], 'u2r',regex=True)
df['label'] = df['label'].replace(['back.', 'land.' , 'neptune.', 'pod.', 'smurf.', 'teardrop.'], 'dos',regex=True)
但是,这仍然是打印数据帧的前 5 行的输出:
After replacing, first 5 rows of df: 

duration protocol_type ... dst_host_srv_rerror_rate label
0 0 tcp ... 0.00 normal
1 0 udp ... 0.00 normal
2 0 tcp ... 0.00 neptune
3 0 tcp ... 0.01 normal
4 0 tcp ... 0.00 normal
我期望第 2 行中的标签列读取“dos”而不是“neptune”,但它没有发生。
我究竟做错了什么?任何帮助表示赞赏。

最佳答案

也许您正在使用 "neptune."而不是 "neptune"我的测试似乎适用于 "neptune"

>>> df
label
0 neptune
>>> df["label"].replace(["neptune."], "normal", regex=True)
0 neptune
Name: label, dtype: object
>>> df["label"].replace(["neptune"], "normal", regex=True)
0 normal
Name: label, dtype: object
>>> df["label"].replace(["neptune"], "normal")
0 normal
Name: label, dtype: object

关于python - 如何替换 Pandas 中的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63787308/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com