I have one single column that looks like this
None header |
---|
time(s) |
0.2055 |
0.2302 |
position(m) |
0.4164 |
0.4128 |
time(s) |
0.2055 |
0.2302 |
position(m) |
0.4164 |
0.4128 |
And I want split this column into columns like this:
time(s) | position(m) |
---|---|
... | ... |
CodePudding user response:
You can use numpy reshaping:
N = len(df)//4
m = np.tile(np.repeat([False,True], [1,N-1]), len(df)//N)
# array([False, True, True, False, True, True, False, True, True,
# False, True, True])
df2 = pd.DataFrame(df.iloc[m, 0]
.to_numpy()
.reshape((2,-1,N-1))
.swapaxes(1,2)
.reshape(-1,2),
columns=df.iloc[[0,N],0].to_list()
)
print(df2)
Output:
time(s) position(m)
0 0.2055 0.4164
1 0.2302 0.4128
2 0.2055 0.4164
3 0.2302 0.4128
CodePudding user response:
Use:
df['g'] = df.index//3%2
temp = df.groupby('g')['header'].apply(list).to_frame().T.explode([0,1]).reset_index(drop=True)
temp.columns = temp.iloc[0]
temp[~temp['time(s)'].str.contains('[a-z]')]
Output:
time(s) position(m)
1 0.2055 0.4164
2 0.2302 0.4128
4 0.2055 0.4164
5 0.2302 0.4128