2017-08-11 41 views
-1

我這裏顯示的數據幀:Python腳本移調數據幀

time 0:15 0:30 0:45 
1/1/12 27.84 28.08 27.6 

我需要將其調換到像一個在這裏的另一個數據框: 時間

1/1/12 0:15 27.84 
1/1/12 0:30 28.08 
1/1/12 0:45 27.6 

任何想法,我如何進行?

+0

您是否嘗試過搜索˚F或者任何方法? – roganjosh

+1

如果只有一種叫做'transpose()'的方法... ... –

+0

是的,有很多想法。看看列(df.columns),它們變成了你使用的系列,第一行的值成爲另一個系列,最後在時間之下的索引或值成爲新的索引。把拼圖放在一起,你就在那裏。 –

回答

-2
import numpy as np 
a = np.array([[1, 2], [3, 4]]) 
a 
array([[1, 2], 
     [3, 4]]) 
a.transpose() 
array([[1, 3], 
     [2, 4]]) 

通過這種方式,您可以移調在python

1

陣列中使用numpy的,如果你的DF看起來像這樣(df.set_index(「時間」),如果它有一個單獨的列的時間)。

df 

time 0:15 0:30 0:45   
1/1/12 27.84 28.08 27.6 

你可以簡單:

# 1. Create a new DataFrame with two rows (columns and row1) - transpose them. 
# 2. Set index to the old index to list and multiply by length of df2 

df2 = pd.DataFrame([df.columns,df.iloc[0]]).T 
df2.index = df.index.tolist()*len(df2) 
df2 

其中給出:

 0  1 
1/1/12 0:15 27.84 
1/1/12 0:30 28.08 
1/1/12 0:45 27.6 
因爲這個

作品:

df.columns # Index(['0:15', '0:30', '0:45'], dtype='object') 
df.iloc[0] # array([ 27.84, 28.08, 27.6 ]) 
df.index.tolist()*len(df.columns) # ['1/1/12', '1/1/12', '1/1/12']