使用Python中的for循环结果创建数据帧

时间:2017-07-20 12:23:16

标签: python for-loop dataframe

当我在

下面运行for循环时
for i in df:
    d = df[pd.notnull(df[i])]
    c = df[df[i]>str(1)].count()
    print(c)

我在下面得到了一个结果

1     244
2     122
3      53
4      75
dtype: int64
1     122
2     206
3      62
4      77
dtype: int64

我想创建一个如下所示的数据框

   1   2
1 244 122
2 122 206
3  53  62
4  75  77

有人可以帮我编码吗?

1 个答案:

答案 0 :(得分:1)

Having an excerpt of your initial data would be useful, but from what I can gather you're only trying to append the two arrays together to obtain a pandas dataframe. That can be easily done with something like:

data = pd.DataFrame()

for i in df:
    d = df[pd.notnull(df[i])]
    c = df[df[i]>str(1)].count()
    data = data.join(c)

print(data)