[英]How to Conditionally Fill Pandas Column based on Cell Values in another column
I have a dataframe of around 10000 rows and want to fill multiple columns based on certain conditions.我有一个大约 10000 行的 dataframe 并且想根据某些条件填充多个列。
if Operating System Contains "Windows Server", so Platform takes server or contains ('Windows 7|Windows 10') than Platform takes "Workstation"如果操作系统包含“Windows Server”,则平台采用服务器或包含('Windows 7|Windows 10'),而不是平台采用“工作站”
Code I Have Tried:我尝试过的代码:
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())
Error I am facing:我面临的错误:
[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
Not an efficient method but will get the job done不是一种有效的方法,但可以完成工作
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')
you can drop the index or reset it if you want to如果需要,您可以删除索引或重置它
You can try this: `你可以试试这个:`
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
`
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.