首页 > 解决方案 > 使用其他行的值添加新行

问题描述

我有一个熊猫数据框。我想在特定位置添加一个新行,例如在第一行之后。我希望使用第 1 行和第 2 行中的值来分配新行中单元格的值,但前提是两行中的值都不等于零。

States  Andhra Pradesh  Bihar   Chhattisgarh    Delhi
1973-74    50.67        56.01         0         60.99
1977-78    69.66        57.45         0         95.85
1983       115.4        93.75       100        217.14
1993-94    288.7        218.3       130        605.22
import pandas as pd
import math
data1= pd.read_excel('C:\\Users\\3004\\Desktop\\Variables.xlsx',3,index_col = 0)
data1.loc[8] = math.exp(math.log(data1.loc['1977-78']/data1.loc['1973-74'])/4)*data1.loc['1973-74']

预期输出:

States  Andhra Pradesh  Bihar   Chhattisgarh    Delhi
1973-74    50.67        56.01         0         60.99
1975       54.87        56.37         0         68.29
1977-78    69.66        57.45         0         95.85
1983       115.4        93.75       100        217.14
1993-94    288.7        218.3       130        605.22

第 1975 行的第一个值是“EXP(LN(69.66/50.67)/4)*50.57”

但是,我收到一条错误消息"TypeError: cannot convert the series to <class 'float'>"

标签: python-3.xpandas

解决方案


推荐阅读