【问题标题】:Create a dataframe using for loop results in Python在 Python 中使用 for 循环结果创建数据框
【发布时间】:2017-07-21 00:41:27
【问题描述】:

当我运行下面的 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

有人可以帮我编码吗?

【问题讨论】:

标签: python for-loop dataframe


【解决方案1】:

摘录您的初始数据会很有用,但据我所知,您只是试图将两个数组附加在一起以获得熊猫数据框。这可以通过以下方式轻松完成:

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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-15
    相关资源
    最近更新 更多