Created
August 27, 2017 06:35
-
-
Save sonesuke/232e7446ecfc958ac5cd385af9bac3b0 to your computer and use it in GitHub Desktop.
change row and col in pandas
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pandas as pd | |
def f(a): | |
a.index = [0 for i in range(len(a))] | |
del a['rot'] | |
out = a[0:1] | |
for i in range(1, len(a)): | |
out = out.join(a[i:i+1], rsuffix='{0}'.format(i)) | |
return out | |
df = pd.read_csv('rot.csv', index_col=0) | |
df = df.groupby('rot').apply(f) | |
df = pd.DataFrame(df.to_records()) | |
del df['level_0'] | |
print(df) | |
''' | |
https://ja.stackoverflow.com/questions/24845/pythonのpandasで-縦持ちのデータを横持ちにするよい方法を教えてください | |
,rot,x,y | |
0,rot1,0.0,1.0 | |
1,rot1,0.89619220103,0.443666021702 | |
2,rot1,0.795220057023,-0.606320922374 | |
3,rot1,-0.190567962875,-0.981674004711 | |
4,rot1,-0.964317116929,-0.264749878183 | |
5,rot1,-0.665101514979,0.746752954311 | |
6,rot1,0.374151230571,0.927367703051 | |
7,rot1,0.997097890944,0.0761301246241 | |
8,rot1,0.510605678474,-0.859815004004 | |
9,rot1,-0.544021110889,-0.839071529076 | |
0,rot2,0.0998334166468,0.995004165278 | |
1,rot2,0.932039085967,0.362357754477 | |
2,rot2,0.745705212177,-0.66627602128 | |
3,rot2,-0.255541102027,-0.966798192579 | |
4,rot2,-0.977530117665,-0.210795799431 | |
5,rot2,-0.631266637872,0.77556587851 | |
6,rot2,0.404849920617,0.914383148235 | |
7,rot2,0.998543345375,0.0539554205626 | |
8,rot2,0.501020856458,-0.865435209241 | |
9,rot2,-0.544021110889,-0.839071529076 | |
0,rot3,0.479425538604,0.87758256189 | |
1,rot3,0.999883861694,0.0152401812223 | |
2,rot3,0.505948760832,-0.862563534711 | |
3,rot3,-0.501277048588,-0.865286842936 | |
4,rot3,-0.999951654067,0.00983308337127 | |
5,rot3,-0.484164059469,0.874977236 | |
6,rot3,0.52281341913,0.85244714134 | |
7,rot3,0.999390803641,-0.0349001661689 | |
8,rot3,0.46207497714,-0.886840862557 | |
9,rot3,-0.544021110889,-0.839071529076 | |
rot x y x1 y1 x2 y2 x3 | |
0 rot1 0.000000 1.000000 0.896192 0.443666 0.795220 -0.606321 -0.190568 | |
1 rot2 0.099833 0.995004 0.932039 0.362358 0.745705 -0.666276 -0.255541 | |
2 rot3 0.479426 0.877583 0.999884 0.015240 0.505949 -0.862564 -0.501277 | |
y3 x4 ... x5 y5 x6 y6 \ | |
0 -0.981674 -0.964317 ... -0.665102 0.746753 0.374151 0.927368 | |
1 -0.966798 -0.977530 ... -0.631267 0.775566 0.404850 0.914383 | |
2 -0.865287 -0.999952 ... -0.484164 0.874977 0.522813 0.852447 | |
x7 y7 x8 y8 x9 y9 | |
0 0.997098 0.076130 0.510606 -0.859815 -0.544021 -0.839072 | |
1 0.998543 0.053955 0.501021 -0.865435 -0.544021 -0.839072 | |
2 0.999391 -0.034900 0.462075 -0.886841 -0.544021 -0.839072 | |
[3 rows x 21 columns] | |
''' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment