Python Forum
pandas change value two dataframe
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
pandas change value two dataframe
good morning i'm trying to find the differences between two dataframes the promo 'df1' has only one column which is 'codbar, where the equivalent in' def2 'and' 'sku' in practice I have to find the equivalences of codbar in the sku column for then change the values on the other two columns stock_quantity and stok_status. i'm trying but i can't do it i need help

import pandas as pd

data1 = pd.read_csv('eurogold.csv' )

data2 = pd.read_csv('woo.csv')

df1 = pd.DataFrame(data1,columns=['codbar'])
df2 = pd.DataFrame(data2,columns=['sku','stock_quantity','stock_status'])

have tried this, but not work well.

import pandas as pd

df1 = pd.DataFrame({'codbar': ['k5', 'K3', 'k2']})

df2 = pd.DataFrame({'stock_status': ['C0', 'C2', 'C3','cd'],
                    'stock_quantity': ['D0', 'D2', 'D3', 'gg'],
                    'sku': ['K0', 'K2', 'K3', 'k5']})

def change_cols(x):
    if x['sku'] in df1.codbar.tolist():
        x['stock_quantity'] = '1'
        x['stock_status'] = '2'

df2.apply(change_cols, axis=1)

Big Grin
You capitalized only 'K3':
'codbar': ['k5', 'K3', 'k2']

and sku 'k5' not capitalized
'sku': ['K0', 'K2', 'K3', 'k5']
ok you are right my mistake. but there I tested on my datframes extracted from two csv but no code changes (the first two codes are the same in all and two tables) I share my project. the script in question is

I have found a solution:

import pandas as pd

data1 = pd.read_csv('eurogold.csv',index_col=0)

data2 = pd.read_csv('woo.csv', index_col=0)

df1 = pd.DataFrame(data1)

print ('stampa df1')
df1 = df1.drop_duplicates(['codbar'])  # elimina duplicati
df1['codbar'] = df1['codbar'].astype(str)

df2 = pd.DataFrame(data2)

df2 = df2.dropna().reset_index(drop=True)
print ('stampa df2')

mask = df2.sku.isin(df1.codbar.values)
df2.loc[mask, 'stock_quantity'] = '0'
df2.loc[mask, 'stock_status'] = 'outofstock'


Possibly Related Threads…
Thread Author Replies Views Last Post
  Partial Matching Rows In Pandas DataFrame Query eddywinch82 1 203 Jul-08-2021, 06:32 PM
Last Post: eddywinch82
  Pandas dictionary dataframe help michaelserra 4 321 Jun-19-2021, 10:26 AM
Last Post: michaelserra
  Pandas DataFrame combine rows by column value, where Date Rows are NULL rhat398 0 344 May-04-2021, 10:51 PM
Last Post: rhat398
Star Split and organize my Pandas Dataframe brunolelli 4 605 Apr-18-2021, 03:00 AM
Last Post: brunolelli
  Convert MultiLayer XML to DataFrame using Pandas vsingh17 0 498 Apr-14-2021, 03:50 PM
Last Post: vsingh17
  Pandas DataFrame Code Query eddywinch82 6 1,022 Feb-12-2021, 09:55 PM
Last Post: eddywinch82
  Pandas dataframe without index tgottsc1 3 1,618 Feb-01-2021, 05:29 PM
Last Post: snippsat
  DataFrame operations didn't change orginal HoldYourBreath 5 538 Jan-10-2021, 02:01 PM
Last Post: jefsummers
  Json File more pages #pandas #dataframe nio74maz 0 485 Dec-30-2020, 05:32 AM
Last Post: nio74maz
  Pandas Extract data from two dataframe nio74maz 1 485 Dec-26-2020, 09:52 PM
Last Post: nio74maz

Forum Jump:

User Panel Messages

Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020