[英]How to rearrange and map data from one CSV to another CSV using python
我已經使用 Amazon textract 從具有多個表的圖像中提取表數據並嘗試將所有提取的數據 map 到 Output 模板 CSV 中,
但是,在提取的輸入 CSV 文件中有多個表,它們分別列在另一個下面。 在每個 CSV 中,大約有 7 個表在下面列出。
請建議如何 map 從輸入 CSV 到 Output 的值。
輸入 CSV 文件:
S.No Item Item_code 1st 2nd 3rd 4th Avg
1 Math_book BK001 27 36 35 23 30
2 Phy_book BJ008 30 40 40 30 35
3 Hin_book NK103 50 50 30 30 40
4 Che_book CH001 40 40 40 20 35
S.No Item_Name Item_code 1st 2nd 3rd 4th Avg
1 Math_book BK001 27 36 35 23 30
2 Phy_book BJ008 30 40 40 30 35
3 Hin_book NK103 50 50 30 30 40
S.No Product Item_code 1st 2nd 3rd 4th Avg
1 Phy_book BJ008 30 40 40 30 35
2 Hin_book NK103 50 50 30 30 40
3 Che_book CH001 40 40 40 20 35
4 Bio_book BI005 50 30 40 60 45
預期 output:
S.No Product Item_code 1st 2nd 3rd 4th
1 Math_book BK001 54 72 70 46
2 Phy_book BJ008 90 120 120 90
3 Hin_book NK103 150 150 90 90
4 Che_book CH001 80 80 80 60
5 Bio_book BI005 50 30 40 60
我一直在嘗試使用的代碼:
df = pd.read_csv(r'input.csv')
df2 = pd.read_csv(r'output.csv')
我如何按組添加所有值通過考慮( Item,Item_Name,Product )列提交df2中的值
請建議
只需使用 grouby -
df = df[df['S.No']!='S.No'].drop('S.No',axis=1) # drop header rows
df[df.columns.values[2:]] = df[df.columns.values[2:]].apply(lambda x: x.astype(int)) #convert data to int type
df = df.groupby(['Item','Item_code'],as_index=False).sum()
df.to_csv('out.csv',index_label='S.No', sep= '\t') # specify the name of output file here
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.