在for循环

时间:2018-01-23 02:31:03

标签: python list pandas for-loop dataframe

pandas DataFrame我以:

开头

pandas DataFrame I'm starting with

pandas DataFrame我试图建立:

pandas DataFrame I'm trying to build

我对计算机科学非常陌生,所以我不确定如何在不提供图像的情况下提出问题。基本上,我想构建一个pandas DataFrame,其中一行包含列名为-3到3的列,下面的值是第一个pandas DataFrame中与第一个pandas第一列相关的第二列的最大绝对值数据帧。

我在此处显示的列表中也有相同的数据:

I also have the same data in a list a shown here

以下是我尝试过但我一直收到错误的信息:

Here is what I've tried but I keep getting an error

1 个答案:

答案 0 :(得分:0)

这是解决方案。循环数据框以获得你想要的东西似乎有点过分

import pandas as pd
df = pd.DataFrame([[-1,1],[-2,2],[-2,1],[-2,2],[-1,6],[-1,2],[-1,1],[1,-2],[2,-2],[1,-2],[2,-1],[6,-1],[2,-1],[1,-1]])
max = df.groupby(0)[1].max()
x = dict()
for i in range(-3,4):
    try:
        if y[i] < 0:
            x[i] = z[i]
        else:
            x[i] = y[i]
    except KeyError:
        x[i] = 0
x = pd.DataFrame(x, index = [0])

给出结果

-3  -2  -1  0   1   2   3
0   2   6   0   -2  -2  0

这会产生一个数据框,其中有一列为&#39; 0&#39; - 任何时候都应该很容易摆脱