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
  Structuring and pivoting corrupted dataframe in pandas gunner1905 2 127 Sep-18-2021, 01:30 PM
Last Post: gunner1905
  TypeError: 'DataFrame' object is not callable using Pandas in Python sofiavlachou 1 154 Sep-02-2021, 03:24 PM
Last Post: buran
  Problem in saving .xlsm (excel) file using pandas dataframe in python shantanu97 2 227 Aug-29-2021, 12:39 PM
Last Post: snippsat
  Partial Matching Rows In Pandas DataFrame Query eddywinch82 1 328 Jul-08-2021, 06:32 PM
Last Post: eddywinch82
  Pandas dictionary dataframe help michaelserra 4 469 Jun-19-2021, 10:26 AM
Last Post: michaelserra
  Pandas DataFrame combine rows by column value, where Date Rows are NULL rhat398 0 471 May-04-2021, 10:51 PM
Last Post: rhat398
Star Split and organize my Pandas Dataframe brunolelli 4 726 Apr-18-2021, 03:00 AM
Last Post: brunolelli
  Convert MultiLayer XML to DataFrame using Pandas vsingh17 0 625 Apr-14-2021, 03:50 PM
Last Post: vsingh17
  Pandas DataFrame Code Query eddywinch82 6 1,157 Feb-12-2021, 09:55 PM
Last Post: eddywinch82
  Pandas dataframe without index tgottsc1 3 3,138 Feb-01-2021, 05:29 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

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