Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/345.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 Pandas-设置数据上的DateTimeIndex_Python_Pandas - Fatal编程技术网

Python Pandas-设置数据上的DateTimeIndex

Python Pandas-设置数据上的DateTimeIndex,python,pandas,Python,Pandas,熔化、设置列名和仅获取值: price size 0 6759.0 19493 1 6758.5 39015 2 6758.0 31137 3 6757.5 30 4 6757.0 2730 5 6756.5 1290 6 6756.0 4287 7 6755.5 20117 8 6755.0 227173 9 6754.5 368844 10 6754.0 618665 11 6753.5 9000 12 675

熔化、设置列名和仅获取值:

    price   size
0   6759.0  19493
1   6758.5  39015
2   6758.0  31137
3   6757.5  30
4   6757.0  2730
5   6756.5  1290
6   6756.0  4287
7   6755.5  20117
8   6755.0  227173
9   6754.5  368844
10  6754.0  618665
11  6753.5  9000
12  6753.0  28846
13  6752.5  72021
14  6752.0  229463
15  6751.5  110
16  6751.0  13008
17  6750.5  15150
18  6750.0  65950
19  6749.5  19916
要生成最终df,我要设置以下索引:

df = df.melt().T
df.columns = [colnames]
df = df[-1:]
这在过去对我很有效,但在我尝试使用此df时设置新索引时会出现
ValueError:Must pass DataFrame with boolean value only
错误

    sell_price_10   sell_price_9    sell_price_8    sell_price_7    sell_price_6    sell_price_5    sell_price_4    sell_price_3    sell_price_2    sell_price_1    buy_price_1 buy_price_2 buy_price_3 buy_price_4 buy_price_5 buy_price_6 buy_price_7 buy_price_8 buy_price_9 buy_price_10    sell_size_10    sell_size_9 sell_size_8 sell_size_7 sell_size_6 sell_size_5 sell_size_4 sell_size_3 sell_size_2 sell_size_1 buy_size_1  buy_size_2  buy_size_3  buy_size_4  buy_size_5  buy_size_6  buy_size_7  buy_size_8  buy_size_9  buy_size_10
value   6759    6758.5  6758    6757.5  6757    6756.5  6756    6755.5  6755    6754.5  6754    6753.5  6753    6752.5  6752    6751.5  6751    6750.5  6750    6749.5  19493   39015   31137   30  2730    1290    4287    20117   227173  368844  618665  9000    28846   72021   229463  110 13008   15150   65950   19916

可以通过简单地传递与数据帧长度相同的iterable来设置索引

从初始数据帧开始

df['time'] = pd.to_datetime(round(time.time(),0), unit='s')
df.set_index(df['time'], inplace=True)
df.drop(['time'],axis=1, inplace=True)
首先将索引设置为最终需要的列名

df = pd.DataFrame({
    'price': [6759.0, 6758.5, 6758.0, 6757.5, 6757.0, 6756.5, 
              6756.0, 6755.5, 6755.0, 6754.5, 6754.0, 6753.5, 
              6753.0, 6752.5, 6752.0, 6751.5, 6751.0, 6750.5, 
              6750.0, 6749.5],
    'size': [19493, 39015, 31137, 30, 2730, 1290, 4287, 20117, 
             227173, 368844, 618665, 9000, 28846, 72021, 229463, 
             110, 13008, 15150, 65950, 19916]
})
然后根据当前的df转置构造一个新的数据帧

a, b = zip(*[('sell_price_%d' % i, 'buy_price_%d' % i) for i in range(1,11)])

df.index = a+b # a+b would be your colnames
并设置其索引

df2 = df.T[:1]

是否试图用当前时间替换
值(行索引)?是的,正确(字符限制)
df2.index = [pd.to_datetime(round(time.time(),0), unit='s')]

df2
# outputs:
                     sell_price_1  sell_price_2  sell_price_3  sell_price_4  \
2018-04-10 01:27:59        6759.0        6758.5        6758.0        6757.5

                     sell_price_5  sell_price_6  sell_price_7  sell_price_8  \
2018-04-10 01:27:59        6757.0        6756.5        6756.0        6755.5

                     sell_price_9  sell_price_10  buy_price_1  buy_price_2  \
2018-04-10 01:27:59        6755.0         6754.5       6754.0       6753.5

                     buy_price_3  buy_price_4  buy_price_5  buy_price_6  \
2018-04-10 01:27:59       6753.0       6752.5       6752.0       6751.5

                     buy_price_7  buy_price_8  buy_price_9  buy_price_10
2018-04-10 01:27:59       6751.0       6750.5       6750.0        6749.5