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.melt().T
df.columns = [colnames]
df = df[-1:]
要生成最终的df,我想设置索引:
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
过去这对我有用,但在尝试使用此df设置新索引时会出现ValueError: Must pass DataFrame with boolean values only
错误。
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)
答案 0 :(得分:1)
可以通过简单地传递一个与数据帧长度相同的迭代来设置索引。
从您的初始数据框开始
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]
然后从当前的df转置
构造一个新的DataFramedf2.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
并设置其索引
{{1}}