gpt4 book ai didi

python - 从多列中选择两列

转载 作者:行者123 更新时间:2023-12-01 00:06:22 27 4
gpt4 key购买 nike

我有一个如下所示的数据集(文件名:自定义):

1 1 65.39 0.372404 -0.788955 -2.8455 -1.09717
2 1 65.39 0.367242 1.60121 -0.460432 -1.53554
10 2 16 -0.352459 -1.66367 -1.20991 -0.382338
12 2 16 -0.386501 1.15155 -2.3643 -0.924258
6 1 65.39 0.368096 -1.6016 0.453956 -1.53428
15 2 16 -0.387579 -1.15008 2.35207 -0.919574
5 1 65.39 0.374176 0.792712 2.83707 -1.08298
9 2 16 -0.354946 1.70919 1.21546 -0.390097
3 1 65.39 0.363083 -1.30479 -0.834649 1.57459
11 2 16 -0.375158 -0.403614 -2.52878 0.871748
4 1 65.39 0.370609 1.60183 -2.52762 1.05139
16 2 16 -0.357762 2.05067 -0.683076 0.438964
7 1 65.39 0.371291 -1.61835 2.52926 1.04944
13 2 16 -0.357278 -2.06082 0.680406 0.438092
8 1 65.39 0.365109 1.29671 0.840627 1.5585
14 2 16 -0.380327 0.38719 2.546 0.884682

我想选择第二列和第四列,然后在第二列中排序:

1 0.372404
1 0.367242
...
2 -0.352459
2 -0.386501
...

为此我已经尝试过

with open(custom,"r") as f:
noa = f.readline()
noa = int(noa) <------- ValueError: invalid literal for int () with base 10
config = [ [ 0 for i in range(4) ] for j in range(self.noa) ]
for i in range(noa):
dummy = f.readline()
spliter = dummy.split()
config[i][1] = float(spliter[1])
config[i][3] = float(spliter[3])

我尝试过 len(noa) 但它不起作用。

另外,如何像上面的表格一样在列中“排序”?

最佳答案

我会将您的数据读入数据帧,并在 pandas 中处理它。像这样:

import pandas as pd
df = pd.read_csv(custom, delimiter='')

然后您可以像这样访问列:

col_1 = df.iloc[0]

其中 0 代表第一列,1 代表第二列,依此类推。

关于python - 从多列中选择两列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59948764/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com