我是Python和Pandas的新手。
我有一个数据帧df_pos_alpha
,该数据帧的对称翼型为正角alpha
。数据框有59行和11列,看起来像:
alpha Re1 Re2 Re3 Re4 Re5 Re6 Re7 Re8 Re9 Re10
0 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000
1 0.0434 0.0891 0.1054 0.1100 0.1100 0.1100 0.1100 0.1100 0.1100 0.1100
2 0.0715 0.1740 0.2099 0.2200 0.2200 0.2200 0.2200 0.2200 0.2200 0.2200
3 0.0725 0.2452 0.3078 0.3300 0.3300 0.3300 0.3300 0.3300 0.3300 0.3300
4 0.0581 0.3041 0.4186 0.4400 0.4400 0.4400 0.4400 0.4400 0.4400 0.4400
... .... .... .... .... .... .... .... .... .... ....
145 -0.9000 -0.9000 -0.9000 -0.9000 -0.9000 -0.9000 -0.9000 -0.9000 -0.9000 -0.9000
150 -0.7700 -0.7700 -0.7700 -0.7700 -0.7700 -0.7700 -0.7700 -0.7700 -0.7700 -0.7700
155 -0.6700 -0.6700 -0.6700 -0.6700 -0.6700 -0.6700 -0.6700 -0.6700 -0.6700 -0.6700
160 -0.6350 -0.6350 -0.6350 -0.6350 -0.6350 -0.6350 -0.6350 -0.6350 -0.6350 -0.6350
165 -0.6800 -0.6800 -0.6800 -0.6800 -0.6800 -0.6800 -0.6800 -0.6800 -0.6800 -0.6800
170 -0.8500 -0.8500 -0.8500 -0.8500 -0.8500 -0.8500 -0.8500 -0.8500 -0.8500 -0.8500
175 -0.6600 -0.6600 -0.6600 -0.6600 -0.6600 -0.6600 -0.6600 -0.6600 -0.6600 -0.6600
180 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000
由于机翼是对称的,因此其负角特性与正角相同(Re1
至Re10
的列alpha
和-alpha
相同)
我想获得一个alpha
从-180到180的新数据帧。
我可以实现以下目标:
创建数据框的另一个实例df_neg_alpha
,更改
第一列alpha
的符号,按升序alpha
进行排序。
将df_pos_alpha
追加到df_neg_alpha
。
我的问题:有没有快速的方法或内置方法来实现?
答案 0 :(得分:2)
如果我对您的理解正确,我们可以复制df_pos_alpha
,更改alpha
列的符号,然后使用pandas.concat
将两个数据帧附加到axis=0
上(例如:彼此之间),同时使用sort_values
升序:
df_neg_alpha = df_pos_alpha.copy()
df_neg_alpha['alpha'] = -df_neg_alpha['alpha']
df_new = pd.concat([df_neg_alpha.sort_values('alpha'),
df_pos_alpha],
ignore_index=True).drop_duplicates('alpha')
print(df_new)
alpha Re1 Re2 Re3 Re4 Re5 Re6 Re7 Re8 Re9 Re10
0 -180 0.0000 0.0000 0.0000 0.000 0.000 0.000 0.000 0.000 0.000 0.000
1 -175 -0.6600 -0.6600 -0.6600 -0.660 -0.660 -0.660 -0.660 -0.660 -0.660 -0.660
2 -170 -0.8500 -0.8500 -0.8500 -0.850 -0.850 -0.850 -0.850 -0.850 -0.850 -0.850
3 -165 -0.6800 -0.6800 -0.6800 -0.680 -0.680 -0.680 -0.680 -0.680 -0.680 -0.680
4 -160 -0.6350 -0.6350 -0.6350 -0.635 -0.635 -0.635 -0.635 -0.635 -0.635 -0.635
5 -155 -0.6700 -0.6700 -0.6700 -0.670 -0.670 -0.670 -0.670 -0.670 -0.670 -0.670
6 -150 -0.7700 -0.7700 -0.7700 -0.770 -0.770 -0.770 -0.770 -0.770 -0.770 -0.770
7 -145 -0.9000 -0.9000 -0.9000 -0.900 -0.900 -0.900 -0.900 -0.900 -0.900 -0.900
8 -4 0.0581 0.3041 0.4186 0.440 0.440 0.440 0.440 0.440 0.440 0.440
9 -3 0.0725 0.2452 0.3078 0.330 0.330 0.330 0.330 0.330 0.330 0.330
10 -2 0.0715 0.1740 0.2099 0.220 0.220 0.220 0.220 0.220 0.220 0.220
11 -1 0.0434 0.0891 0.1054 0.110 0.110 0.110 0.110 0.110 0.110 0.110
12 0 0.0000 0.0000 0.0000 0.000 0.000 0.000 0.000 0.000 0.000 0.000
14 1 0.0434 0.0891 0.1054 0.110 0.110 0.110 0.110 0.110 0.110 0.110
15 2 0.0715 0.1740 0.2099 0.220 0.220 0.220 0.220 0.220 0.220 0.220
16 3 0.0725 0.2452 0.3078 0.330 0.330 0.330 0.330 0.330 0.330 0.330
17 4 0.0581 0.3041 0.4186 0.440 0.440 0.440 0.440 0.440 0.440 0.440
18 145 -0.9000 -0.9000 -0.9000 -0.900 -0.900 -0.900 -0.900 -0.900 -0.900 -0.900
19 150 -0.7700 -0.7700 -0.7700 -0.770 -0.770 -0.770 -0.770 -0.770 -0.770 -0.770
20 155 -0.6700 -0.6700 -0.6700 -0.670 -0.670 -0.670 -0.670 -0.670 -0.670 -0.670
21 160 -0.6350 -0.6350 -0.6350 -0.635 -0.635 -0.635 -0.635 -0.635 -0.635 -0.635
22 165 -0.6800 -0.6800 -0.6800 -0.680 -0.680 -0.680 -0.680 -0.680 -0.680 -0.680
23 170 -0.8500 -0.8500 -0.8500 -0.850 -0.850 -0.850 -0.850 -0.850 -0.850 -0.850
24 175 -0.6600 -0.6600 -0.6600 -0.660 -0.660 -0.660 -0.660 -0.660 -0.660 -0.660
25 180 0.0000 0.0000 0.0000 0.000 0.000 0.000 0.000 0.000 0.000 0.000