通过熊猫的数据框架值和indecies迭代

发布于 2025-01-26 08:24:23 字数 454 浏览 1 评论 0 原文

我有以下数据框:
“
而且,对于数据框中的每一列,我都在尝试计算与简单线性回归有关的以下方程:
这些公式要求我能够参考每个列中的两个值(x_i)以及他们的行索引/名称(y_i),以及行索引和实际列值的平均值。我该怎么做?我知道这是一个经验法则,永远不要在数据框架上迭代,但是,考虑到此框架的尺寸很小,我真的不在乎它是如何完成的。

I've got the following dataframe:
dataframe
and, for every column in the dataframe, I'm attempting to calculate the following equations relating to simple linear regression:equations
These formulas require me to be able to reference both the values in each column (x_i) and their row index/name (y_i), along with the mean of both the row indices and the actual column values. How can I do this? I know that it's a rule of thumb never to iterate over a dataframe, but, given the small size of this frame, I really don't care how it's done.

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

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

发布评论

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

评论(1

暮色兮凉城 2025-02-02 08:24:23

您可以通过 apply()函数进行操作。您可以访问每行的“ x”值和“ y”值(索引)。

示例数据框:

df = pd.DataFrame(index=range(1920, 1931), data={'AUS': range(15,26), 'CAN': range(12,23)})
df

      AUS  CAN
1920   15   12
1921   16   13
1922   17   14
1923   18   15
1924   19   16
1925   20   17
1926   21   18
1927   22   19
1928   23   20
1929   24   21
1930   25   22

然后访问 apply()函数中每一行的值:

df.apply(lambda x: [x.name, x['AUS'], x['CAN']], axis=1)

1920    [1920, 15, 12]
1921    [1921, 16, 13]
1922    [1922, 17, 14]
1923    [1923, 18, 15]
1924    [1924, 19, 16]
1925    [1925, 20, 17]
1926    [1926, 21, 18]
1927    [1927, 22, 19]
1928    [1928, 23, 20]
1929    [1929, 24, 21]
1930    [1930, 25, 22]

敲除这些方程将取决于您。

You could do it via an apply() function. You can access the 'x' values and the 'y' value (index) for each row.

Sample DataFrame:

df = pd.DataFrame(index=range(1920, 1931), data={'AUS': range(15,26), 'CAN': range(12,23)})
df

      AUS  CAN
1920   15   12
1921   16   13
1922   17   14
1923   18   15
1924   19   16
1925   20   17
1926   21   18
1927   22   19
1928   23   20
1929   24   21
1930   25   22

Then access then values for each row in an apply() function:

df.apply(lambda x: [x.name, x['AUS'], x['CAN']], axis=1)

1920    [1920, 15, 12]
1921    [1921, 16, 13]
1922    [1922, 17, 14]
1923    [1923, 18, 15]
1924    [1924, 19, 16]
1925    [1925, 20, 17]
1926    [1926, 21, 18]
1927    [1927, 22, 19]
1928    [1928, 23, 20]
1929    [1929, 24, 21]
1930    [1930, 25, 22]

Knocking out those equations will be up to you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文