python jupyter在if语句中测试的相同条件的行为不同
拥有一个 jupyter实验室笔记本,在某个点比较两个数据范围。 df_lastweek 仅是上周数据的提取,而 df_lastmonth 是最后30天的提取。这两个数据范围不同,后者的行与前者更多。
以下如果比较两个不同的数据范围不会触发:
if not df_lastweek.equals(df_lastmonth):
df_lastmonth.describe()
下一个单元格使用相同的语句并按预期触发: 如果不是df_lastweek.equals(df_lastmonth): regplot_of_df(df_lastmonth,2000) 并定期调用regplot_of_df函数,按预期绘制数据。
尝试反转两个单元格,但描述语句永远不会被调用。
关于我缺少的东西没有任何线索。遵循有关两个数据范围的更多数据,同样,如果有人关心检查所有笔记本,您可能会在此处找到它: ,其中“令人讨厌”的细胞是最后三个: [https://github.com/rjalexa/blood-pressure/blob/master/bplogs_analyze.ipynb] [1]
df_lastweek.info()
<class 'pandas.core.frame.DataFrame'>
Int64Index: 10 entries, 0 to 9
Data columns (total 12 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 Date 10 non-null object
1 partofday 10 non-null object
2 Time 10 non-null object
3 SYS 10 non-null int64
4 DIA 10 non-null int64
5 Pulse 10 non-null int64
6 Measurement Date 10 non-null object
7 datetime 10 non-null datetime64[ns]
8 unix 10 non-null float64
9 elapsed_seconds 10 non-null float64
10 bp_stage 10 non-null object
11 bp_color 10 non-null object
dtypes: datetime64[ns](1), float64(2), int64(3), object(6)
memory usage: 1.0+ KB
df_lastmonth.info()
<class 'pandas.core.frame.DataFrame'>
Int64Index: 21 entries, 0 to 20
Data columns (total 12 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 Date 21 non-null object
1 partofday 21 non-null object
2 Time 21 non-null object
3 SYS 21 non-null int64
4 DIA 21 non-null int64
5 Pulse 21 non-null int64
6 Measurement Date 21 non-null object
7 datetime 21 non-null datetime64[ns]
8 unix 21 non-null float64
9 elapsed_seconds 21 non-null float64
10 bp_stage 21 non-null object
11 bp_color 21 non-null object
dtypes: datetime64[ns](1), float64(2), int64(3), object(6)
memory usage: 2.1+ KB
[1]: https://github.com/rjalexa/blood-pressure/blob/master/bplogs_analyze.ipynb
Have a Jupyter Lab notebook which at a certain point compares two dataframes. df_lastweek is an extraction of only last week's data while the df_lastmonth is the extraction of the last 30 days. The two dataframes are different the latter having more rows than the former.
The following if comparing the two different dataframes does not trigger:
if not df_lastweek.equals(df_lastmonth):
df_lastmonth.describe()
while the next cell uses the SAME statement and does trigger as expected:
if not df_lastweek.equals(df_lastmonth):
regplot_of_df(df_lastmonth, 2000)
and regularly call the regplot_of_df function plotting the data as expected.
Tried inverting the two cells but the describe statement never gets called.
No clue as to what I'm missing. Follows some more data about the two dataframes and also if anyone cares to check all of the notebook you may find it here:
, where the "offending" cells are the very last three:
[https://github.com/rjalexa/blood-pressure/blob/master/bplogs_analyze.ipynb][1]
df_lastweek.info()
<class 'pandas.core.frame.DataFrame'>
Int64Index: 10 entries, 0 to 9
Data columns (total 12 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 Date 10 non-null object
1 partofday 10 non-null object
2 Time 10 non-null object
3 SYS 10 non-null int64
4 DIA 10 non-null int64
5 Pulse 10 non-null int64
6 Measurement Date 10 non-null object
7 datetime 10 non-null datetime64[ns]
8 unix 10 non-null float64
9 elapsed_seconds 10 non-null float64
10 bp_stage 10 non-null object
11 bp_color 10 non-null object
dtypes: datetime64[ns](1), float64(2), int64(3), object(6)
memory usage: 1.0+ KB
df_lastmonth.info()
<class 'pandas.core.frame.DataFrame'>
Int64Index: 21 entries, 0 to 20
Data columns (total 12 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 Date 21 non-null object
1 partofday 21 non-null object
2 Time 21 non-null object
3 SYS 21 non-null int64
4 DIA 21 non-null int64
5 Pulse 21 non-null int64
6 Measurement Date 21 non-null object
7 datetime 21 non-null datetime64[ns]
8 unix 21 non-null float64
9 elapsed_seconds 21 non-null float64
10 bp_stage 21 non-null object
11 bp_color 21 non-null object
dtypes: datetime64[ns](1), float64(2), int64(3), object(6)
memory usage: 2.1+ KB
[1]: https://github.com/rjalexa/blood-pressure/blob/master/bplogs_analyze.ipynb
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果
有效,它只是不显示结果,因为它不是单元格中的最后一个表达式。您需要使用display
:另外,您可以设置
Interactiveshell.ast_node_interactivity
'all'。The
if
works, it just doesn't show the result as it's not the last expression in the cell. You need do usedisplay
:Alternatively, you may set
InteractiveShell.ast_node_interactivity
to'all'
.