Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.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 熊猫:计算两行之间的百分比,并将该值作为列添加_Python_Pandas_Dataframe - Fatal编程技术网

Python 熊猫:计算两行之间的百分比,并将该值作为列添加

Python 熊猫:计算两行之间的百分比,并将该值作为列添加,python,pandas,dataframe,Python,Pandas,Dataframe,我的数据集结构如下: "Date","Time","Open","High","Low","Close","Volume" 这个时间序列代表普通股票市场的价值 我想计算“Close”列两行之间的百分比差异(事实上,我想知道股票价值增加或减少了多少;每行代表一天) 我使用for循环完成了这项工作(在大数据问题中使用pandas非常糟糕),我在不同的数据帧中创建了正确的结果: rows_number = df_stock.shape[0] # The first row will be 1, b

我的数据集结构如下:

"Date","Time","Open","High","Low","Close","Volume"
这个时间序列代表普通股票市场的价值

我想计算“Close”列两行之间的百分比差异(事实上,我想知道股票价值增加或减少了多少;每行代表一天)

我使用for循环完成了这项工作(在大数据问题中使用pandas非常糟糕),我在不同的数据帧中创建了正确的结果:

rows_number = df_stock.shape[0]

# The first row will be 1, because is calculated in percentage. If haven't any yesterday the value must be 1
percentage_df = percentage_df.append({'Date': df_stock.iloc[0]['Date'], 'Percentage': 1}, ignore_index=True)

# Foreach days, calculate the market trend in percentage
for index in range(1, rows_number):

    # n_yesterday : 100 = (n_today - n_yesterday) : x
    n_today = df_stock.iloc[index]['Close']
    n_yesterday = self.df_stock.iloc[index-1]['Close']
    difference = n_today - n_yesterday
    percentage = (100 * difference ) / n_yesterday

    percentage_df = percentage_df .append({'Date': df_stock.iloc[index]['Date'], 'Percentage': percentage}, ignore_index=True)

我如何利用dataFrame api重构它,从而删除for循环并创建一个新列?

使用
diff

(-df['Close'].diff())/df['Close'].shift()

我建议首先将Date列作为您可以使用的DateTime索引

df_stock = df_stock.set_index(['Date'])
df_stock.index = pd.to_datetime(df_stock.index, dayfirst=True)
然后,只需使用datetime索引访问具有特定列的任何行,并执行任何类型的操作,例如,计算“Close”列中两行之间的百分比差异

还可以使用for循环对每个日期或行执行操作:

for Dt in df_stock.index:

df['Change']=df['Close'].pct\u Change()

或者,如果要按相反顺序计算更改:


df['Change']=df['Close'].pct\u Change(-1)

你能解释一下它是如何工作的吗?我该如何实现呢?检查一下,为什么会有一个负号?你需要
n_今天-n_昨天
diff give
n_昨天-n_今天
根据给定的链接,情况正好相反。还有:[pd.DataFrame.pct_change]()。来自pandas的文档:。如何仅用于列关闭,并在新列中添加值?
df['change']=df['Close'].pct_change()
for Dt in df_stock.index: