gpt4 book ai didi

python - 从行索引和列索引创建数据框

转载 作者:行者123 更新时间:2023-12-02 11:35:46 25 4
gpt4 key购买 nike

由于我之前的问题没有得到解答,我会更简单地再试一次。

我有一个程序可以创建一个包含行和列索引的列表:

a = [0, 0, 1, 1] # row
b = [0, 1, 0, 1] # column

这也可以格式化为列表列表:

a = [[0,0], [0,1], [1,0], [1,1]]

数据框:

      Name  2544  2543  2542
0 DR01 13 16 23
1 DR02 15 27 12
2 DR03 16 18 13
3 DR04 17 92 56

目标是根据列表中的索引从提到的数据帧中获取值并创建一个新的数据帧。我尝试了 for 循环,但没有成功,而且速度非常慢。

它应该是什么样子:

      Name  2544
0 DR01 13
1 DR02 15

更新:

这是我尝试过的:

In[96]: df = importDialog.df.iloc[a,b]
In[96]: print(df)

Out[96]:
Name 2544 Name 2544
0 DR01 13 DR01 13
1 DR02 15 DR02 15

此外,当我尝试在表格中显示完整的数据集时:

MemoryError: Unable to allocate 9.06 GiB for an array with shape (34749, 34992) and data type object

最佳答案

假设您检查了 a,b 构成矩形:

a = [0, 0, 1, 1] # row
b = [0, 1, 0, 1] # column

df.iloc[sorted(set(a)),sorted(set(b))]

输出:

   Name  2544
0 DR01 13
1 DR02 15

关于python - 从行索引和列索引创建数据框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60328442/

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