Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/325.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_Arrays_Numpy_Dataframe_Indexing - Fatal编程技术网

python中的数组/数据帧操作

python中的数组/数据帧操作,python,arrays,numpy,dataframe,indexing,Python,Arrays,Numpy,Dataframe,Indexing,是否有一种有效的方法来创建输出以下内容的数组或数据帧(不需要最左边和最上面的行中的Price列): 输入将是一个包含1列的csv文件,由日期列表索引。因此,对于上述示例,它将是: Date Price 12/18/1992 18.00 12/21/1992 18.50 12/22/1992 17.25 我试图计算每个按时间顺序排列的日期对的价格变化。所以日期1/日期0、日期2/日期0等等。我想在不按时间顺序排列的日期对中留空 到目前为止我只有这个…: import pa

是否有一种有效的方法来创建输出以下内容的数组或数据帧(不需要最左边和最上面的行中的Price列):

输入将是一个包含1列的csv文件,由日期列表索引。因此,对于上述示例,它将是:

Date    Price
12/18/1992   18.00 
12/21/1992   18.50 
12/22/1992   17.25 
我试图计算每个按时间顺序排列的日期对的价格变化。所以日期1/日期0、日期2/日期0等等。我想在不按时间顺序排列的日期对中留空

到目前为止我只有这个…:

import pandas as pd

import numpy as np

import datetime

import matplotlib.pyplot as plt


file_loc = "C:\\Users\\Price Data\\CL1.csv"

df = pd.read_csv(file_loc, parse_dates = True)
df.set_index('Date', inplace = True)

看起来您想创建一个新的数据帧,其中包含第一帧中所有数据的成对比较。您还希望这个新框架中的列和索引都用price和date标记。您可以通过在提供的代码之后添加以下内容来实现这一点:

# Get the data for your columns and indices
prices = df['Price']
dates = df.index

# create column/index data as a list
table_labels = list(zip(prices,dates))

# create a dataframe
pairwise_df = pd.DataFrame(columns=table_labels,index=table_labels)

# fill it with your data
# a percentage in the upper triangle, or an empty space in the lower triangle
for p1,d1 in table_labels:    
    for p2,d2 in table_labels: 
        pairwise_df.loc[(p1,d1),(p2,d2)] = (p2-p1)/p1*100 if d2 >= d1 else ''
print(pairwise_df)

这应该可以实现我列出的两个目标。

您可以像这样使用pandas和numpy函数:

df_out = (pd.crosstab([df['Price'],df['Date']],[df['Price'],df['Date']])
            .apply(lambda x: (x.name[0]-x.index.get_level_values(0))/
                              x.index.get_level_values(0)*100).round(0).astype(int)
            .sort_index(level=1)
            .sort_index(level=1, axis=1))

df_out = df_out.where(np.triu(np.ones(df_out.shape, dtype=bool)))

df_out
输出:

Price                 18.00      18.50      17.25      12.50      14.50
Date             12/18/1992 12/21/1992 12/22/1992 12/23/1992 12/24/1992
Price Date                                                             
18.00 12/18/1992        0.0        3.0       -4.0      -31.0        -19
18.50 12/21/1992        NaN        0.0       -7.0      -32.0        -22
17.25 12/22/1992        NaN        NaN        0.0      -28.0        -16
12.50 12/23/1992        NaN        NaN        NaN        0.0         16
14.50 12/24/1992        NaN        NaN        NaN        NaN          0

这太棒了!想知道是否有办法让这个过程更快-我有5000个日期,所以它需要永远运行。你能矢量化内部循环吗?一次计算整行。
Price                 18.00      18.50      17.25      12.50      14.50
Date             12/18/1992 12/21/1992 12/22/1992 12/23/1992 12/24/1992
Price Date                                                             
18.00 12/18/1992        0.0        3.0       -4.0      -31.0        -19
18.50 12/21/1992        NaN        0.0       -7.0      -32.0        -22
17.25 12/22/1992        NaN        NaN        0.0      -28.0        -16
12.50 12/23/1992        NaN        NaN        NaN        0.0         16
14.50 12/24/1992        NaN        NaN        NaN        NaN          0