Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/354.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用Dataframe中的值从Dataframe中的函数生成新列_Python_Pandas_Dataframe_Apply - Fatal编程技术网

Python 使用Dataframe中的值从Dataframe中的函数生成新列

Python 使用Dataframe中的值从Dataframe中的函数生成新列,python,pandas,dataframe,apply,Python,Pandas,Dataframe,Apply,我是熊猫数据帧的新手,我有点挣扎,因为我不知道如何访问一个特定的单元格来计算填充一个新的单元格 我想使用apply调用一个外部函数,其中包含第1行单元格中的数据 我这样做了,但在一个简单的数组中输出所有内容,但我非常确定有更好的方法: 我使用以下索引从csv构建数据帧: DateIndex = pd.date_range(start="2005-1-1", end="2017-1-1", freq=BDay()) 我确信我的数据帧是正常的,根据以下摘录: 2005-01-03 0.005

我是熊猫数据帧的新手,我有点挣扎,因为我不知道如何访问一个特定的单元格来计算填充一个新的单元格

我想使用apply调用一个外部函数,其中包含第1行单元格中的数据

我这样做了,但在一个简单的数组中输出所有内容,但我非常确定有更好的方法:

我使用以下索引从csv构建数据帧:

DateIndex = pd.date_range(start="2005-1-1", end="2017-1-1", freq=BDay())
我确信我的数据帧是正常的,根据以下摘录:

2005-01-03    0.005742
2005-01-04    0.003765
2005-01-05   -0.005536
2005-01-06    0.001500
2005-01-07    0.007471
2005-01-10    0.002108
2005-01-11   -0.003195
2005-01-12   -0.003076
2005-01-13    0.005416
2005-01-14    0.003090
所以,我想在第一个条目上加100,对于其他条目,加1,然后乘以前一个条目

我可以在阵列中执行此操作:

for i in range(0,len(df.index)):
    if i == 0:
        listV = [df.iloc[i] + 100]
    else:
        listV.append(listV[i-1] * (1 + df.iloc[i]))
有没有办法做到这一点并将结果直接放入数据框的新列中

非常感谢, 当做
朱利安

这里有一个更好的方法来实现同样的目标:

col_copy = df.col.copy()   # generate a copy to isolate the series completely
col_copy.iloc[0] += 100    # Increment first row by 100
col_copy.iloc[1:] += 1     # Increment 1 to rest

df.assign(new_col=col_copy.cumprod()) # compute cumulative product and assign to new column
收益率:

数据:

考虑一个带有单列
'Col'
DF
,如下所示:

txt = StringIO(
"""
2005-01-03    0.005742
2005-01-04    0.003765
2005-01-05   -0.005536
2005-01-06    0.001500
2005-01-07    0.007471
2005-01-10    0.002108
2005-01-11   -0.003195
2005-01-12   -0.003076
2005-01-13    0.005416
2005-01-14    0.003090
""")

df = pd.read_csv(txt, delim_whitespace=True, parse_dates=True, header=None, 
                 index_col=['date'], names=['date', 'col'])
df.index.name = None
df
初始化

df = pd.DataFrame(dict(
        col=[ 0.005742,  0.003765, -0.005536,  0.0015  ,  0.007471,
              0.002108, -0.003195, -0.003076,  0.005416,  0.00309 ]
    ), pd.to_datetime([
            '2005-01-03', '2005-01-04', '2005-01-05', '2005-01-06', '2005-01-07', 
            '2005-01-10', '2005-01-11', '2005-01-12', '2005-01-13', '2005-01-14'])
    )

print(df)

                 col
2005-01-03  0.005742
2005-01-04  0.003765
2005-01-05 -0.005536
2005-01-06  0.001500
2005-01-07  0.007471
2005-01-10  0.002108
2005-01-11 -0.003195
2005-01-12 -0.003076
2005-01-13  0.005416
2005-01-14  0.003090

评论
这看起来是一系列的回报。通过将
100
添加到第一次观察中,您将第一次返回边缘化,使其成为
.57
基点,而不是
.57
百分比

我相信你要做的是把所有的东西加一,然后取累积积,然后乘以100

这将显示累计增长100
,这是我相信您所追求的

df.add(1).cumprod().mul(100)

                   col
2005-01-03  100.574200
2005-01-04  100.952862
2005-01-05  100.393987
2005-01-06  100.544578
2005-01-07  101.295746
2005-01-10  101.509278
2005-01-11  101.184956
2005-01-12  100.873711
2005-01-13  101.420043
2005-01-14  101.733431


只需执行
df['new column name']=listV
。您需要删除if语句中的方括号,否则它会将值转换为列表。这一行也应该在append语句中,就像在else语句中一样。这是领域知识发挥作用的地方;-)明亮的就是这样!我对你感激不尽。
df.add(1).cumprod().mul(100).plot()