Python 如何复制datafram的最后一行并对复制的行进行更改?

Python 如何复制datafram的最后一行并对复制的行进行更改?,python,python-3.x,pandas,Python,Python 3.x,Pandas,我有以下数据帧: Commodity Code Commodity Year Reporter Reporter ISO Partner Partner ISO Export Import Re-Export Re-Import 1564774 270900 Petroleum oils & oils obt. from bitum

我有以下数据帧:

        Commodity Code                                          Commodity  Year   Reporter Reporter ISO         Partner Partner ISO        Export  Import  Re-Export  Re-Import
1564774         270900  Petroleum oils & oils obt. from bituminous min...  2013  Venezuela          VEN         Curaçao         CUW  0.000000e+00     2.0        0.0        0.0
1563032         270900  Oils; petroleum oils and oils obtained from bi...  2013  Venezuela          VEN         Belgium         BEL  2.404415e+08     0.0        0.0        0.0
1563066         270900  Oils; petroleum oils and oils obtained from bi...  2013  Venezuela          VEN          Brazil         BRA  1.067652e+08     0.0        0.0        0.0
1563121         270900  Oils; petroleum oils and oils obtained from bi...  2013  Venezuela          VEN          Canada         CAN  3.170861e+08     0.0        0.0        0.0
1563177         270900  Oils; petroleum oils and oils obtained from bi...  2013  Venezuela          VEN           China         CHN  1.014889e+10     0.0        0.0        0.0
1563353         270900  Oils; petroleum oils and oils obtained from bi...  2013  Venezuela          VEN          France         FRA  0.000000e+00    33.0        0.0        0.0
1563397         270900  Oils; petroleum oils and oils obtained from bi...  2013  Venezuela          VEN         Germany         DEU  2.722320e+08     0.0        0.0        0.0
1563486         270900  Oils; petroleum oils and oils obtained from bi...  2013  Venezuela          VEN           India         IND  1.494257e+10     0.0        0.0        0.0
1563707         270900  Oils; petroleum oils and oils obtained from bi...  2013  Venezuela          VEN        Malaysia         MYS  5.509827e+08     0.0        0.0        0.0
1563760         270900  Oils; petroleum oils and oils obtained from bi...  2013  Venezuela          VEN     Netherlands         NLD  4.208213e+08     0.0        0.0        0.0
1563840         270900  Oils; petroleum oils and oils obtained from bi...  2013  Venezuela          VEN       Nicaragua         NIC  3.877573e+07     0.0        0.0        0.0
1564055         270900  Oils; petroleum oils and oils obtained from bi...  2013  Venezuela          VEN       Singapore         SGP  2.000000e+00     0.0        0.0        0.0
1564129         270900  Oils; petroleum oils and oils obtained from bi...  2013  Venezuela          VEN           Spain         ESP  1.559714e+09     0.0        0.0        0.0
1564185         270900  Oils; petroleum oils and oils obtained from bi...  2013  Venezuela          VEN          Sweden         SWE  6.652943e+08     0.0        0.0        0.0
1564281         270900  Oils; petroleum oils and oils obtained from bi...  2013  Venezuela          VEN             USA         USA  2.815425e+10     0.0        0.0        0.0
1564372         270900  Oils; petroleum oils and oils obtained from bi...  2013  Venezuela          VEN  United Kingdom         GBR  3.303344e+08     0.0        0.0        0.0
1563307         270900  Oils; petroleum oils and oils obtained from bi...  2013  Venezuela          VEN  Dominican Rep.         DOM  6.038139e+08     0.0        0.0        0.0
1563590         270900  Oils; petroleum oils and oils obtained from bi...  2013  Venezuela          VEN           Japan         JPN  4.834996e+08     0.0        0.0        0.0
1564420         270900  Oils; petroleum oils and oils obtained from bi...  2013  Venezuela          VEN         Uruguay         URY  4.908717e+08     0.0        0.0        0.0
1564655         270900  Petroleum oils & oils obt. from bituminous min...  2013  Venezuela          VEN         Jamaica         JAM  7.417692e+08     0.0        0.0        0.0
我想复制数据帧的最后一行并更改值。我确实复制了最后一行:

other_country_row = group.iloc[-1:]
这给了我:

            Commodity Code                                          Commodity  Year   Reporter Reporter ISO  Partner Partner ISO       Export  Import  Re-Export  Re-Import
    1564655         270900  Petroleum oils & oils obt. from bituminous min...  2013  Venezuela          VEN  Jamaica         JAM  741769216.0     0.0        0.0        0.0

and I changed values on it, unfortunately, it is not copied and making changes on the last row of the data frame.

        Commodity Code                                          Commodity  Year   Reporter Reporter ISO         Partner Partner ISO        Export  Import  Re-Export  Re-Import
1564774         270900  Petroleum oils & oils obt. from bituminous min...  2013  Venezuela          VEN         Curaçao         CUW  0.000000e+00     2.0        0.0        0.0
1563032         270900  Oils; petroleum oils and oils obtained from bi...  2013  Venezuela          VEN         Belgium         BEL  2.404415e+08     0.0        0.0        0.0
1563066         270900  Oils; petroleum oils and oils obtained from bi...  2013  Venezuela          VEN          Brazil         BRA  1.067652e+08     0.0        0.0        0.0
1563121         270900  Oils; petroleum oils and oils obtained from bi...  2013  Venezuela          VEN          Canada         CAN  3.170861e+08     0.0        0.0        0.0
1563177         270900  Oils; petroleum oils and oils obtained from bi...  2013  Venezuela          VEN           China         CHN  1.014889e+10     0.0        0.0        0.0
1563353         270900  Oils; petroleum oils and oils obtained from bi...  2013  Venezuela          VEN          France         FRA  0.000000e+00    33.0        0.0        0.0
1563397         270900  Oils; petroleum oils and oils obtained from bi...  2013  Venezuela          VEN         Germany         DEU  2.722320e+08     0.0        0.0        0.0
1563486         270900  Oils; petroleum oils and oils obtained from bi...  2013  Venezuela          VEN           India         IND  1.494257e+10     0.0        0.0        0.0
1563707         270900  Oils; petroleum oils and oils obtained from bi...  2013  Venezuela          VEN        Malaysia         MYS  5.509827e+08     0.0        0.0        0.0
1563760         270900  Oils; petroleum oils and oils obtained from bi...  2013  Venezuela          VEN     Netherlands         NLD  4.208213e+08     0.0        0.0        0.0
1563840         270900  Oils; petroleum oils and oils obtained from bi...  2013  Venezuela          VEN       Nicaragua         NIC  3.877573e+07     0.0        0.0        0.0
1564055         270900  Oils; petroleum oils and oils obtained from bi...  2013  Venezuela          VEN       Singapore         SGP  2.000000e+00     0.0        0.0        0.0
1564129         270900  Oils; petroleum oils and oils obtained from bi...  2013  Venezuela          VEN           Spain         ESP  1.559714e+09     0.0        0.0        0.0
1564185         270900  Oils; petroleum oils and oils obtained from bi...  2013  Venezuela          VEN          Sweden         SWE  6.652943e+08     0.0        0.0        0.0
1564281         270900  Oils; petroleum oils and oils obtained from bi...  2013  Venezuela          VEN             USA         USA  2.815425e+10     0.0        0.0        0.0
1564372         270900  Oils; petroleum oils and oils obtained from bi...  2013  Venezuela          VEN  United Kingdom         GBR  3.303344e+08     0.0        0.0        0.0
1563307         270900  Oils; petroleum oils and oils obtained from bi...  2013  Venezuela          VEN  Dominican Rep.         DOM  6.038139e+08     0.0        0.0        0.0
1563590         270900  Oils; petroleum oils and oils obtained from bi...  2013  Venezuela          VEN           Japan         JPN  4.834996e+08     0.0        0.0        0.0
1564420         270900  Oils; petroleum oils and oils obtained from bi...  2013  Venezuela          VEN         Uruguay         URY  4.908717e+08     0.0        0.0        0.0
1564655         270900  Petroleum oils & oils obt. from bituminous min...  2013  Venezuela          VEN           Other       Other  1.478252e+10     0.0        0.0        0.0
1564655         270900  Petroleum oils & oils obt. from bituminous min...  2013  Venezuela          VEN           Other       Other  1.478252e+10     0.0        0.0        0.0

正如您所看到的,Jamaice已被删除(我希望保留jamaica并添加最后一行)。如何处理此问题?

使用切片调用
.iloc
将返回对基础数据的可变引用。您需要的是:

other\u country\u row=group.iloc[-1:].copy()

默认情况下,这将返回数据的深度副本,您可以在不影响原始数据帧的情况下安全地编辑该副本。

使用iloc分配新值:
df.iloc[-1]=[…]