我有名为‘hasta_2019’的数据:
infl_gen alim_proc alim_fres bienes ener serv
fecha
2001-01 71.94 63.25 68.91 87.30 59.91 69.34
2001-02 71.94 63.36 68.60 87.18 60.21 69.45
2001-03 72.52 63.43 69.43 88.64 60.15 69.70
2001-04 73.17 63.49 69.65 90.06 61.12 70.10
2001-05 73.43 63.81 69.99 90.35 62.21 70.06
2001-06 73.63 63.95 70.16 90.26 62.63 70.38
2001-07 73.11 64.07 70.72 87.88 60.99 70.88
2001-08 73.17 64.23 71.11 87.65 60.10 71.30
2001-09 73.56 64.58 71.36 89.02 60.21 71.01
2001-10 73.89 65.09 70.89 91.00 58.88 71.03
2001-11 74.08 65.43 70.89 92.12 57.45 71.02
2001-12 74.28 65.70 71.77 92.06 56.75 71.39
2002-01 74.15 65.89 71.94 89.78 58.17 72.04
2002-02 74.21 66.00 71.78 89.53 58.32 72.41
2002-03 74.86 66.25 72.21 90.33 59.43 73.02
2002-04 75.90 67.20 72.71 92.54 60.90 73.28
2002-05 76.16 67.47 73.03 92.96 61.06 73.50
2002-06 76.16 67.63 73.17 92.91 59.87 73.80
2002-07 75.64 67.74 73.60 89.96 60.07 74.23
2002-08 75.90 67.81 74.15 89.82 60.26 74.71
2002-09 76.16 67.95 74.73 90.71 60.77 74.43
2002-10 76.87 68.01 74.85 93.38 61.08 74.42
2002-11 77.00 68.07 75.01 94.69 59.49 74.27
2002-12 77.26 68.26 75.62 94.56 60.04 74.61
2003-01 76.94 68.66 75.92 91.62 61.35 74.96
2003-02 77.07 69.14 75.21 91.57 62.20 75.25
2003-03 77.65 69.38 75.47 92.50 63.05 75.62
2003-04 78.30 69.47 75.43 95.07 61.39 76.17
2003-05 78.24 69.53 75.76 95.52 59.86 76.01
2003-06 78.30 69.59 75.93 95.36 59.52 76.32
2003-07 77.85 69.67 76.71 92.04 60.06 76.86
2003-08 78.24 69.80 77.73 91.81 60.88 77.44
2003-09 78.43 69.92 78.83 92.74 60.63 77.01
2003-10 78.95 70.23 78.54 94.90 59.96 77.12
2003-11 79.21 70.43 78.53 95.94 60.14 76.99
2003-12 79.34 70.57 78.96 95.86 59.96 77.29
2004-01 78.69 70.96 79.25 92.38 60.34 77.59
2004-02 78.76 71.37 78.26 92.21 60.60 77.90
2004-03 79.34 71.57 78.77 93.05 61.49 78.31
2004-04 80.45 72.00 78.98 95.90 62.29 78.94
2004-05 80.90 72.82 79.48 96.49 63.80 78.83
2004-06 81.03 73.00 79.47 96.43 63.79 79.18
2004-07 80.45 73.05 80.04 92.89 64.00 79.69
2004-08 80.84 73.14 80.36 92.80 65.14 80.25
2004-09 80.97 73.24 80.31 93.79 65.18 79.87
2004-10 81.81 73.29 80.25 96.27 66.92 79.90
2004-11 82.00 73.42 80.66 97.23 66.12 79.89
2004-12 81.94 73.55 80.89 97.06 64.50 80.18
2005-01 81.16 73.88 81.54 93.35 63.99 80.50
2005-02 81.35 73.96 81.20 93.16 65.23 80.88基本上,我真正想要的是选择每一列,然后进行以下操作:(一年中的第一个月/下一年的下一个月)- 1。我想把它转换成一个函数,这样我就可以将该列作为参数添加,然后就会产生神奇的效果。但是我得到了一个错误: NameError: name 'column1‘没有定义
因此,我有以下代码行:
def function(new_columna):
new = new_columna
new_columna = []
cont = 12
for e in range(0,229,12):
datos = ((hasta_2019.iloc[cont, 0] / hasta_2019.iloc[e, 0] ) - 1)*100
new_columna.append(datos)
cont += 12
if cont >= hasta_2019.shape[0]:
print("terminado")
break
return new_columna使用它,我想创建一个列表,然后用它创建新的dataframe,但是我得到了一个错误
function(_column1_)
--------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-924-fcd7fae2a243> in <module>
----> 1 funcion(Prueba1)
NameError: name '_column1_' is not defined发布于 2020-11-19 17:04:50
首先,在您当前的代码中:
def function(new_columna):
new = new_columna
new_columna = []由于缩进错误,您的函数无法工作:
def function(new_columna):
new = new_columna
new_columna = []但是你不需要一个函数来创建一个简单的列表对象。你只需要一个空的列表,首先:
new_list = []
cont = 12
for e in range(0,229,12):
datos = ((hasta_2019.iloc[cont, 0] / hasta_2019.iloc[e, 0] ) - 1)*100
new_list.append(datos)
cont += 12创建列表之后,现在可以将新列表作为列添加到现有的数据框架(df)中,如下所示:
df['column2'] = new_listhttps://stackoverflow.com/questions/64915791
复制相似问题