Beta
Beta

Reputation: 1746

Updating multiple Cell Values in Dataframe

My dataset is in this form:

df = pd.DataFrame({'ID': [1,2,3,4],
                   'Type': ['A', 'B', 'B', 'B'],
                   'Value': [100, 200, 201, 120]})

I want to update the dataframe in the following way:

df = pd.DataFrame({'ID': [1,2,3,4],
                   'Type': ['A', 'B1', 'B2', 'B3'],
                   'Value': [100, 200, 201, 120]}) 

The code I was trying was:

df[df['Type'] == 'B', df['Value'] == 200] = 'B1'

But I'm getting error:

ValueError: Cannot reindex from a duplicate axis

Can someone please help me solve the problem?

Thanks!

Upvotes: 2

Views: 44

Answers (3)

jezrael
jezrael

Reputation: 862481

If need convert all B values by iterator starting by 1 use np.arange by count Trues by sum and join by +:

m = df['Type'] == 'B'
df.loc[m, 'Type'] += np.arange(1, m.sum()+1).astype(str)
print (df)

   ID Type  Value
0   1    A    100
1   2   B1    200
2   3   B2    201
3   4   B3    120

Upvotes: 1

mozway
mozway

Reputation: 260335

You can use:

df['Type'] = df['Type'].mask(df['Type'].eq('B'),
                             df['Type'] + df.groupby('Type').cumcount().add(1).astype(str)
                            )

Upvotes: 1

U13-Forward
U13-Forward

Reputation: 71560

Try this instead:

df.loc[df['Type'].eq('B') & df['Value'].eq(200), 'Type'] = 'B1'

Upvotes: 1

Related Questions