Python Forum

Full Version: pandas change value two dataframe
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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'])


print(df1.equals(df2))
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']})
print(df2)

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)

print(df2)
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 testDataframe.py

GIT
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)
print(df1)

df2 = pd.DataFrame(data2)

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


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

df2.to_csv('risultato.csv')