Home > Enterprise >  How to append to dataframe based on conditions in other column?
How to append to dataframe based on conditions in other column?

Time:06-13

I have the following code:

import pandas as pd

A = [1, 2, 2, 6, 5]
B = [4, 5, 5, 5, 4]
C = ['-'] * len(A)

d = {'A': A, 'B': B, 'C': C}
df = pd.DataFrame(data=d)

df.loc[df['A'] >= 3, 'C'] = 'A>=3'
df.loc[df['B'] >= 3, 'C'] = 'B>=3'

print(df)

How to append a string to the C column instead of writing a new value?

The current result:

   A  B    C
0  1  4  B>=3
1  2  5  B>=3
2  2  5  B>=3
3  6  5  B>=3
4  5  4  B>=3

The desired result:

   A  B    C
0  1  4  B>=3
1  2  5  B>=3
2  2  5  B>=3
3  6  5  A>=3 B>=3
4  5  4  A>=3 B>=3

CodePudding user response:

IIUC use = for join another conditions:

df['C'] = ''
df.loc[df['A'] >= 3, 'C'] = 'A>=3'
df.loc[df['B'] >= 3, 'C']  = ' B>=3'

df['C'] = df['C'].str.strip()

print(df)
   A  B          C
0  1  4       B>=3
1  2  5       B>=3
2  2  5       B>=3
3  6  5  A>=3 B>=3
4  5  4  A>=3 B>=3

CodePudding user response:

Here is a generic method to perform multiple comparisons, and aggregate as a single string without extra spaces:

cols = {'A': 3, 'B': 3}
m = df[list(cols)].ge(cols)

df['C'] = (m
 .where(m).stack().reset_index()
 .assign(col=lambda d: d['level_1'] '>=' d['level_1'].map(cols).astype(str))
 .groupby('level_0')['col'].agg(' '.join)
)

for a single value:

N = 3
cols = ['A', 'B']

m = df[cols].ge(N)
df['C'] = (m
 .where(m).stack().reset_index()
 .assign(col=lambda d: d['level_1'] f'>={N}')
 .groupby('level_0')['col'].agg(' '.join)
)

output:

   A  B          C
0  1  4       B>=3
1  2  5       B>=3
2  2  5       B>=3
3  6  5  A>=3 B>=3
4  5  4  A>=3 B>=3
  • Related