简体   繁体   English

如何根据另一列中的单元格值有条件地填充 Pandas 列

[英]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

The Resulting dataframe expected is:预期的结果 dataframe 是: 数据框

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.

相关问题 根据pandas中的另一个列值有条件地填充列值 - Conditionally fill column values based on another columns value in pandas 如何在熊猫中有条件地填充列中的空值 - How to fill null values in a column conditionally in pandas 根据熊猫df中其他列的值有条件地填充列 - Conditionally fill column based off values in other columns in a pandas df 熊猫:使用基于其他列值的函数有条件地填充列 - Pandas: Conditionally fill column using a function based on other columns values 如何基于另一列填充 Pandas 中的数字缺失值 - How to Fill Numeric missing Values In Pandas Based On Another Column 如何根据pandas中的列填充缺失值? - how to fill missing values based on column in pandas? 如何根据另一列中的值有条件地替换一列中的 NaN 值 - How to conditionally replace NaN values in a column based on values in another column 根据另一列中的值有条件地填充 pandas 中的空值 - Filling null values in pandas based on value in another column conditionally 在 pandas 中,如何根据一列中的唯一值创建列,然后根据另一列中的值填充它? - In pandas, how do I create columns out of unique values in one column, and then fill it based on values in another column? 如何根据 Pandas 数据框中的另一列值填充列中的缺失值? - How to fill missing values in a column based on another column values in a Pandas dataframe?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM