這與我昨天問的問題非常相似。目的是能夠添加一個功能,允許根據(jù)另一個列中顯示的值創(chuàng)建一個列。例如,當它在指定的文件中找到一個國家代碼時,我希望它創(chuàng)建一個名為“國家代碼總計”的列,并對具有相同國家代碼的每一行的單位數(shù)量求和這是我的腳本目前輸出的內(nèi)容:我想看到的:我的腳本:df['Sum of Revenue'] = df['Units Sold'] * df['Dealer Price'] df['AR Revenue'] = df[] df = df.sort_values(['End Consumer Country', 'Currency Code']) # Sets first value of index by position df.loc[df.index[0], 'Unit Total'] = df['Units Sold'].sum() # Sets first value of index by position df.loc[df.index[0], 'Total Revenue'] = df['Sum of Revenue'].sum() # Sums the amout of Units with the End Consumer Country AR df['AR Total'] = df.loc[df['End Consumer Country'] == 'AR', 'Units Sold'].sum() # Sums the amount of Units with the End Consumer Country AU df['AU Total'] = df.loc[df['End Consumer Country'] == 'AU', 'Units Sold'].sum() # Sums the amount of Units with the End Consumer Country NZ df['NZ Total'] = df.loc[df['End Consumer Country'] == 'NZ', 'Units Sold'].sum()但是,由于我知道將出現(xiàn)在此文件中的國家/地區(qū),因此我已將它們相應(yīng)地添加到我的腳本中以進行查找。我將如何編寫我的腳本,以便如果它找到另一個國家/地區(qū)代碼(例如 GB),它將創(chuàng)建一個名為“GB Total”的列,并將國家/地區(qū)代碼設(shè)置為 GB 的每一行的單位相加。任何幫助將不勝感激!
添加回答
舉報
0/150
提交
取消