Home > Backend Development > Python Tutorial > How to create a new column with values ​​selected based on an existing column?

How to create a new column with values ​​selected based on an existing column?

王林
Release: 2024-02-22 13:40:13
forward
972 people have browsed it

How to create a new column with values ​​selected based on an existing column?

Question content

How to add the color column to the following dataframe so that color='green' If set == 'z', otherwise color='red'?

Type  Set
1     A    Z
2     B    Z           
3     B    X
4     C    Y
Copy after login


Correct Answer


If you only have two choices, use np.where:

df['color'] = np.where(df['set']=='z', 'green', 'red')
Copy after login

For example,

import pandas as pd
import numpy as np

df = pd.dataframe({'type':list('abbc'), 'set':list('zzxy')})
df['color'] = np.where(df['set']=='z', 'green', 'red')
print(df)
Copy after login

Yield

set type  color
0   z    a  green
1   z    b  green
2   x    b    red
3   y    c    red
Copy after login

If you have more than two conditions, use np.select. For example, if you want color to be

  • yellow When (df['set'] == 'z') & (df['type'] == 'a')
  • Otherwise blue When (df['set'] == 'z') & (df['type'] == 'b')
  • Otherwise purple when (df['type'] == 'b')
  • Otherwise black,

Then use

df = pd.dataframe({'type':list('abbc'), 'set':list('zzxy')})
conditions = [
    (df['set'] == 'z') & (df['type'] == 'a'),
    (df['set'] == 'z') & (df['type'] == 'b'),
    (df['type'] == 'b')]
choices = ['yellow', 'blue', 'purple']
df['color'] = np.select(conditions, choices, default='black')
print(df)
Copy after login

produce

Set Type   color
0   Z    A  yellow
1   Z    B    blue
2   X    B  purple
3   Y    C   black
Copy after login

The above is the detailed content of How to create a new column with values ​​selected based on an existing column?. For more information, please follow other related articles on the PHP Chinese website!

source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template