我有一个大约10000行的数据框,并希望根据某些条件填充多列。
如果操作系统包含“ Windows Server”,则平台要使用服务器或包含(“ Windows 7 | Windows 10”),而不是平台要使用“ Workstation”
我尝试过的代码:
conditions = [
(dfADTM['Operating System'].str.contains('Windows Server')),
(dfADTM['Operating System'].str.contains('Windows 10|Windows 7|Windows XP')),
(dfADTM['Operating System'].str.contains('Cisco|SLES|OnTap|unknown'))]
choices = ['Server', 'Workstation', 'Network Appliance']
dfADTM['Platform AD'] = np.select(conditions, choices, default='Check')
print(dfADTM.head())
我面临的错误:
[Running] python -u "c:\Users\Abhinav Kumar\Desktop\weekly\code.py"
Traceback (most recent call last):
File "c:\Users\Abhinav Kumar\Desktop\weekly\code.py", line 36, in <module>
dfADTM['Platform AD'] = np.select(conditions, choices, default='Check')
File "C:\ProgramData\Anaconda3\lib\site-packages\numpy\lib\function_base.py", line 715, in select
'invalid entry {} in condlist: should be boolean ndarray'.format(i))
ValueError: invalid entry 0 in condlist: should be boolean ndarray
[Done] exited with code=1 in 7.725 seconds
答案 0 :(得分:0)
这不是一种有效的方法,但是可以完成工作
df.index
for i in range(0,len(df)):
if df['OS'][i].split(" ")[1]=='Server':
df.set_value(i, 'Platform', 'Server')
if df['OS'][i].split(" ")[1]=='7' or df['OS'][i].split(" ")[1]=='10':
df.set_value(i, 'Platform', 'Workstation')
如果需要,您可以删除索引或将其重置
答案 1 :(得分:0)
您可以尝试以下操作: `
import numpy as np
import pandas as pd
df['Platform']=np.nan #create an empty column in the dataframe
for i in range(len(df)):
a=df['Operating System'][i]
if ('Windows 10' or 'Windows 7' or 'Windows XP') in a:
df['Platform'][i]='Workstation'
elif ('Cisco' or 'SLES' or 'OnTap' or 'unknown') in a:
df['Platform'][i]='Network Appliance'
elif ('Windows Server') in a:
df['Platform'][i]='Server'
else:
df['Platform'][i]='Not mentioned' #For the values which do no fall into any category
`