您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

在遍历pandas DataFrame后,为什么此功能不“使用”?

在遍历pandas DataFrame后,为什么此功能不“使用”?

您可以使用apply以下方法

In [11]: df.apply(lambda row: windchill(row['Temperature'], row['Wind Speed']),
                 axis=1)
Out[11]:
2003-03-01 06:00:00-05:00    24.794589
2003-03-01 07:00:00-05:00    25.136527
2003-03-01 08:00:00-05:00    25.934114
2003-03-01 09:00:00-05:00    28.219431
2003-03-01 10:00:00-05:00    29.505105

In [12]: df['Wind Chill'] = df.apply(lambda row: windchill(row['Temperature'], row['Wind Speed']),
                                    axis=1)

In [13]: df
Out[13]:
                           Day  Temperature  Wind Speed  Year  Wind Chill
2003-03-01 06:00:00-05:00    1        30.27        5.27  2003   24.794589
2003-03-01 07:00:00-05:00    1        30.21        4.83  2003   25.136527
2003-03-01 08:00:00-05:00    1        31.81        6.09  2003   25.934114
2003-03-01 09:00:00-05:00    1        34.04        6.61  2003   28.219431
2003-03-01 10:00:00-05:00    1        35.31        6.97  2003   29.505105
@H_404_4@

为了进一步说明混淆的原因,我认为这是因为行变量是 ,因此更改不会传播:

In [21]: for _, row in df.iterrows(): row['Day'] = 2
@H_404_4@

我们看到,它正在改变 ,该row@H_404_4@变量(S):

In [22]: row
Out[22]:
Day               2.00
Temperature      35.31
Wind Speed        6.97
Year           2003.00
Name: 2003-03-01 10:00:00-05:00
@H_404_4@

Bu他们不更新到DataFrame:

In [23]: df
Out[23]:
                           Day  Temperature  Wind Speed  Year
2003-03-01 06:00:00-05:00    1        30.27        5.27  2003
2003-03-01 07:00:00-05:00    1        30.21        4.83  2003
2003-03-01 08:00:00-05:00    1        31.81        6.09  2003
2003-03-01 09:00:00-05:00    1        34.04        6.61  2003
2003-03-01 10:00:00-05:00    1        35.31        6.97  2003
@H_404_4@

以下内容df@H_404_4@保持不变:

In [24]: row = df.ix[0]  # also a copy

In [25]: row['Day'] = 2
@H_404_4@

而如果我们采取以下 :(我们将看到一个 变化 df@H_404_4@。)

In [26]: row = df.ix[2:3]  # this one's a view

In [27]: row['Day'] = 3
@H_404_4@

请参阅返回视图与副本(在文档中)

其他 2022/1/1 18:38:05 有377人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶