我想比較源檔案和目標檔案的 sku,如果目標 sku 與源檔案匹配,我想復制該行的 meta_title、meta_description 和 description
import pandas as pd
source = pd.read_csv('/content/source-source.csv')
destination = pd.read_csv('/content/out.csv')
for i in range(0, len(source)):
try:
source_sku = source.iloc[i]['sku']
destination_sku = destination.iloc[i]['sku']
source_meta_title = source.iloc[i]['meta_title']
source_meta_description = source.iloc[i]['meta_description']
source_description = source.iloc[i]['description']
if source_sku == destination_sku:
destination.loc[i, 'meta_title'] = source_meta_title
destination.loc[i, 'meta_description'] = source_meta_description
destination.loc[i, 'description'] = source_description
destination.to_csv('merged.csv', index=False, encoding='utf-8-sig')
except ValueError:
break
源檔案.csv
sku meta_title meta_description description
a ab ab ab
b bb bb bb
目的地.csv
sku meta_title meta_description description
a bb bb bb
b ab ab ab
合并的.csv
sku meta_title meta_description description
a ab ab ab
b bb bb bb
我的代碼現在正在運行,但是如果行數更改增加,則會引發錯誤
uj5u.com熱心網友回復:
我想這就是你想要的。它保留來自目標的 SKU 集并替換也在源中的 SKU 的值。
import pandas as pd
source = pd.read_csv('/content/source-source.csv')
destination = pd.read_csv('/content/out.csv')
destination = destination.merge(
source,
how='left',
on=['sku'],
suffixes=('_old', ''),
)
value_cols = ['meta_title', 'meta_description', 'description']
for c in value_cols:
destination[c] = destination[c].fillna(destination[f'{c}_old'])
destination = destination.drop(columns=[
f'{c}_old'
for c
in value_cols
])
destination.to_csv('merged.csv', index=False, encoding='utf-8-sig')
uj5u.com熱心網友回復:
如果在合并的資料集中,您希望將目標和源中的行保留在 ['sku'] 中具有不同的值,則可以嘗試這樣做
import pandas as pd
source = pd.DataFrame({'sku':['a','b', 'c'], 'meta_title':['ab','bb', 'ac'], 'meta_description':['ab','bb', 'ac'], 'description':['ab','bb', 'ac']})
destination = pd.DataFrame({'sku':['a','b', 'd'], 'meta_title':['bb','ab', 'dd'], 'meta_description':['bb','ab', 'dd'], 'description':['bb','ab', 'dd']})
df = source.merge(destination, how='outer', on='sku', indicator=True, suffixes=['', '_2'])
cols = ['sku', 'meta_title', 'meta_description', 'description', '_merge']
df = df[cols].loc[df['sku'].drop_duplicates().index].reset_index(drop=True)
# To check only rows with the same SKUs
df[df['_merge']=='both']
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/398000.html
上一篇:即使在index_reset之后,Concat也創建了Nan值
下一篇:從while回圈匯出到csv檔案
