我如何借助两个列表和一个循环下的变量创建数据框

发布于 2025-01-14 06:53:34 字数 965 浏览 2 评论 0原文

我有一个名为 Strike 的变量,在 Strike 中,变量值会定期更改,因为它处于循环状态。

这是我的数据框 输入图片此处描述代码示例 -

for i in range(len(df.strike)):
    Strike = df.strike.iloc[i]
    list1 = ['0-50', '50-100', '100-150'.......]
    list2 = [2000, 132.4, 1467.40, ..........]
    
    df = []  # Here i have to create dataframe

Strike 包含类似 - 33000, 33100, 33200, 33300....... 的值,因此它至少包含 145 个值。 我想排成行。

我有两个列表,它们也会不时更改,因为它也在循环中。

list1 = ['0-50', '50-100', '100-150'.......]

list1 我想制作列。

并且 list2 包含数值 -

list2 = [2000, 132.4, 1467.40, ..........]

在此处输入图像描述

我需要这种格式的数据框。 列表 1 应该是列名称,列表 2 应该是值,并且罢工变量应该是行。 但我不明白如何创建这个数据框。

I have a variable whose name is Strike, in Strike variable values regularly change because it is under a loop.

This is my Dataframe
enter image description here
code example -

for i in range(len(df.strike)):
    Strike = df.strike.iloc[i]
    list1 = ['0-50', '50-100', '100-150'.......]
    list2 = [2000, 132.4, 1467.40, ..........]
    
    df = []  # Here i have to create dataframe

Strike contains values like - 33000, 33100, 33200, 33300....... so on it contains at least 145 values.
which I want to make rows.

And I have two list which is also changing from time to time because it is also under a loop.

list1 = ['0-50', '50-100', '100-150'.......]

list1 I want to make columns.

and list2 contains numeric values -

list2 = [2000, 132.4, 1467.40, ..........]

enter image description here

I need dataframe in this format.
List 1 should we column name, and list 2 should we values and strike variable should be rows.
but I don't understand how can I create this data frame.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

坐在坟头思考人生 2025-01-21 06:53:34

IIUC您可以使用 DataFrame 直接使用 reshaped numpy 数组作为输入:

# dummy example
list2 = list(range(4*7))

list1 = ['0-50', '50-100', '100-150', '150-200']

# replace by df.strike
strike = [33000, 33100, 33200, 33300, 33400, 33500, 33600]

df = pd.DataFrame(np.array(list2).reshape((-1, len(list1))),
                  index=strike, columns=list1)

输出:

       0-50  50-100  100-150  150-200
33000     0       1        2        3
33100     4       5        6        7
33200     8       9       10       11
33300    12      13       14       15
33400    16      17       18       19
33500    20      21       22       23
33600    24      25       26       27

IIUC you could use the DataFrame constructor directly with a reshaped numpy array as input:

# dummy example
list2 = list(range(4*7))

list1 = ['0-50', '50-100', '100-150', '150-200']

# replace by df.strike
strike = [33000, 33100, 33200, 33300, 33400, 33500, 33600]

df = pd.DataFrame(np.array(list2).reshape((-1, len(list1))),
                  index=strike, columns=list1)

output:

       0-50  50-100  100-150  150-200
33000     0       1        2        3
33100     4       5        6        7
33200     8       9       10       11
33300    12      13       14       15
33400    16      17       18       19
33500    20      21       22       23
33600    24      25       26       27
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文