-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
任何想法,我如何进行?
我这里显示的数据帧: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
任何想法,我如何进行?
import numpy as np
a = np.array([[1, 2], [3, 4]])
a
array([[1, 2],
[3, 4]])
a.transpose()
array([[1, 3],
[2, 4]])
阵列中使用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']
您是否尝试过搜索˚F或者任何方法? – roganjosh
如果只有一种叫做'transpose()'的方法... ... –
是的,有很多想法。看看列(df.columns),它们变成了你使用的系列,第一行的值成为另一个系列,最后在时间之下的索引或值成为新的索引。把拼图放在一起,你就在那里。 –