Home > front end >  Update specific rows and columns without creating duplictes
Update specific rows and columns without creating duplictes

Time:10-02

I want to overwrite/update some values in some rows. That is the initial data

   Foo Bar  vals
0    0   a     0
1    1   b     1
2    2   c     2
3    3   d     3
4    4   e     4

I want to give Foo 2 and 4 new vals. This is the expected output.

   Foo Bar  vals
0    0   a     0
1    1   b     1
2    2   c    78
3    3   d     3
4    4   e    63

But that code using DataFrame.update() doesn't work that way and does create duplicated lines.

#!/usr/bin/env python3
import pandas as pd

df = pd.DataFrame({'Foo': range(5), 'Bar': list('abcde'), 'vals': range(5)})
print(df)

df.update(
    pd.DataFrame({'Foo': [2, 4], 'vals': [78, 63]})
)

print(df)

The result is

   Foo Bar  vals
0  2.0   a  78.0
1  4.0   b  63.0
2  2.0   c   2.0
3  3.0   d   3.0
4  4.0   e   4.0

CodePudding user response:

You need to do this:

update_from = pd.DataFrame({'Foo': [2, 4], 'vals': [78, 63]})
m = df['Foo'].isin(update_from['Foo'])
df.loc[m, 'vals'] = df.loc[m, 'Foo'].map(update_from.set_index('Foo')['vals'])

output:

   Foo Bar  vals
0    0   a     0
1    1   b     1
2    2   c    78
3    3   d     3
4    4   e    63

CodePudding user response:

df['column name'] = df['column name'].replace(['old value'],'new value')

what I mean is

import pandas as pd

df = pd.DataFrame({'Foo': range(5), 'Bar': list('abcde'), 'vals':range(5)})

print(df)

df['vals'] = df['vals'].replace([2],[78])

df['vals'] = df['vals'].replace([4],[63])

print(df) # which prints your desired output

CodePudding user response:

Just filter your Foo series with isin() and set the values with lists.

NB. this method does not check for order, only membership. Use @SomeDude's method if order matters.

import pandas as pd

df = pd.DataFrame({'Foo': range(5), 'Bar': list('abcde'), 'vals': range(5)})

df.loc[df["Foo"].isin([2, 4]), "vals"] = [78, 63]
print(df)

gives

   Foo Bar  vals
0    0   a     0
1    1   b     1
2    2   c    78
3    3   d     3
4    4   e    63
  • Related