{"id":358552,"date":"2024-05-21T00:14:20","date_gmt":"2024-05-21T00:14:20","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=358552"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=358552","title":{"rendered":"<span>Python Lives in Excel<\/span>"},"content":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>There was already news on Habr about this significant event. Indeed, it resembles a retelling of the official Microsoft press release, but that&#8217;s how the &#8216;news&#8217; should be.<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/f33\/555\/183\/f33555183e6e2880114435da3372d244.png\" width=\"1024\" height=\"1024\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/f33\/555\/183\/f33555183e6e2880114435da3372d244.png\"\/><\/figure>\n<h3>My Early Attempts<\/h3>\n<p>For some reason that even I don&#8217;t understand, I started trying to use Python inside Excel a few weeks ago. Unsuccessfully. Despite all my efforts, it didn&#8217;t work: initially, I connected to Microsoft 365 Insider, Beta Channel and didn&#8217;t observe the option there. Then I downloaded an add-in from Excel Labs, the Microsoft Garage project. The feature, although it started, did not work for me, constantly returning errors.<\/p>\n<h3>The Big Update<\/h3>\n<p>And then, on 12.10.2023, I received an email with good news: after updating Office, there is now a &#171;Python in Excel&#187; option in the Formulas menu. Entering &#171;=PY(&#187; in the command line converts the current cell into something resembling a Jupyter Notebook cell.<\/p>\n<p><strong><em>Important Reminder:<\/em><\/strong><em> \u00a0To access and use the &#171;Python in Excel&#187; feature, you need to be part of the &#171;Microsoft 365 Insider, Beta Channel.&#187;<\/em><\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/b84\/6ab\/496\/b846ab496caaea64962dadcea11e4566.png\" width=\"900\" height=\"286\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/b84\/6ab\/496\/b846ab496caaea64962dadcea11e4566.png\"\/><\/figure>\n<h3>How Does It Work?<\/h3>\n<p>The code is executed on Microsoft servers. All you need to do on your local computer is start Excel and enter the familiar magical combination &#171;=PY(&#171;. Then you can enter the code. Of course, copy-paste works. The syntax is Python, with a slight specificity if you need to access data. The &#8216;Ctrl+Enter&#8217; command ignites the code.<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/574\/e1a\/718\/574e1a718ba12ce8180bcdbca8a59261.png\" width=\"900\" height=\"542\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/574\/e1a\/718\/574e1a718ba12ce8180bcdbca8a59261.png\"\/><\/figure>\n<h3>The Big Question: Why?<\/h3>\n<p>Why do we need Python inside Excel at all? This is the main question, a huge \u201cWhy?\u201d Are there any advantages over using the two products independently and exchanging data between them when necessary? Possibly there are, but with a big question mark. And despite all my heavy pondering, I can&#8217;t find an answer, although I&#8217;m trying very hard.<\/p>\n<h3>Potential Advantages<\/h3>\n<p>&#8212; You can avoid installing Python altogether.<\/p>\n<p>&#8212; For simple tasks that are more efficiently solved by code than by built-in Excel tools, you don&#8217;t need to multiply entities: both code and data can live inside one file.<\/p>\n<p>&#8212; Another reason to utilize this feature is as a substitute for Microsoft Excel macros, especially if you&#8217;re well-acquainted with Python but not with macros. Thanks to <a href=\"https:\/\/habr.com\/ru\/users\/alexandershelupinin\/\" rel=\"noopener noreferrer nofollow\">@alexandershelupinin<\/a> who <a href=\"https:\/\/habr.com\/ru\/articles\/769732\/#comment_26091220\" rel=\"noopener noreferrer nofollow\">pointed<\/a> this out.<\/p>\n<p>&#8212; And perhaps something else that I&#8217;m not aware of or haven&#8217;t thought of.<\/p>\n<h3>My Personal Take<\/h3>\n<p>Thinking soberly, I can&#8217;t come up with anything rational and sensible, which would require forgetting Python in Excel as an unnecessary toy. But! Procrastination and\/or curiosity force me to figure out the feature and come up with scenarios for using it. A man is weak (I definitely am), can&#8217;t resist temptations. The result of my weakness is this material.<\/p>\n<h4>First Impressions<\/h4>\n<p>According to my disgusting habit, instead of starting the process by carefully reading relevant documentation, I started exploring the feature by using the simplest and most effective of all methods, namely the &#171;trial and error method&#187;. Which, as often happens, led to several hours of bouncing from one elementary mistake to another and a waste of time and nervous energy. Common sense prevailed and &#8212; wonder of wonders! &#8212; after reading the documentation (see links below), the mystery was solved.\u00a0\u00a0\u00a0\u00a0\u00a0 <\/p>\n<p>As it turned out, the fact that the code runs on Microsoft&#8217;s servers has side effects: time delay, can&#8217;t install third party libraries, accessing data outside the current workbook only via Power Query, can&#8217;t save data outside the current workbook, etc.<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/2a8\/24d\/f8d\/2a824df8d06de27868f14f280e8104fe.png\" width=\"900\" height=\"491\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/2a8\/24d\/f8d\/2a824df8d06de27868f14f280e8104fe.png\"\/><\/figure>\n<p>Plus, Python syntax is often with adjustments for &#171;living inside Excel&#187;. For example, to assign the value from cell B2 to a variable, we write x = xl(&#171;B2&#187;). And of course, writing code inside Excel&#8217;s formula bar is a pleasure for perverts. Although there is nothing stopping you from writing in a text editor and then copy-pasting. With all the big and small &#171;BUTs&#187;.<\/p>\n<p>Let me try it on a real-life use case.<\/p>\n<h4>Epic Fail<\/h4>\n<p>Since mid-January this year, 2023, I&#8217;ve been having a peculiar romance with Garmin, specifically its sports watches, mobile app, and personal account. This wasn&#8217;t your everyday romance; it was a dance of numbers and data. Every week, like clockwork, I would download and process various sets of data\u2014workouts, breathing exercises, and more. Then, I would merge these with records of my resting heart rate and HRV. My trusty Python script managed all this seamlessly. But one day, in a mix of curiosity and perhaps a dash of procrastination, I wondered: Could Excel&#8217;s new Python feature handle this?<\/p>\n<p>With optimism, I dove in. I imagined my CSV file, brimming with raw data from the Activities section of my Garmin account, being effortlessly tamed by this new Excel Python integration. I dreamt of easy summarizations, data pulls, merges, and updates. But reality had other plans. After several grueling hours, I had to admit defeat and put this challenge aside.<\/p>\n<h3>And Now It Worked<\/h3>\n<h4>Working with CSV: reading and processing data<\/h4>\n<p>As part of my \u201cromantic\u201d routine I need to supplement my Garmin data with heart rate readings from the Elite HRV app. The non-PRO version of Elite HRV doesn&#8217;t allow for direct data exports, but with a simple request, they send you a link to download an archive of TXT files. These files contain RR intervals\u2014each representing the milliseconds between heartbeats. A cardiologist may task you with extracting heart rate readings from these intervals and plotting a graph to show heart rate fluctuations over time.<\/p>\n<p>With renewed determination, I tackled the challenge using Python in Excel. There were a few hiccups, especially with the query creation, but in the end, it took me just about fifteen minutes to draft a rather intricate code.<\/p>\n<p><code># read data<\/code><\/p>\n<p><code>rr_intervals = xl(\"EliteHRV_SS\")<\/code><\/p>\n<p><code>rr_intervals.columns = ['rr_intervals']<\/code><\/p>\n<p><code>rr_intervals['rr_intervals']\u00a0 = pd.to_numeric(rr_intervals['rr_intervals'])<\/code><\/p>\n<p><code># calculating heart rate for each interval<\/code><\/p>\n<p><code>rr_intervals['heart_rate'] = (60000 \/ rr_intervals['rr_intervals']).astype(int)\u00a0<\/code><\/p>\n<p><code># Calculate cumulative time in seconds for each RR interval<\/code><\/p>\n<p><code>cumulative_time = np.cumsum(rr_intervals['rr_intervals'].values) \/ 1000\u00a0 # Convert from ms to s<\/code><\/p>\n<p><code>cumulative_time = [int(t) for t in cumulative_time]\u00a0<\/code><\/p>\n<p><code>heart_rates = rr_intervals['heart_rate'].values\u00a0<\/code><\/p>\n<p><code>df = pd.DataFrame({<\/code><\/p>\n<p><code>\u00a0\u00a0\u00a0 'timepoints': cumulative_time,<\/code><\/p>\n<p><code>\u00a0\u00a0\u00a0 'heart_rate': heart_rates<\/code><\/p>\n<p><code>\u00a0\u00a0\u00a0 })<\/code><\/p>\n<p><code># Plotting<\/code><\/p>\n<p><code>plt.figure(figsize=(15, 6))<\/code><\/p>\n<p><code>plt.plot(cumulative_time, heart_rates, label='Heart Rate', color='blue')<\/code><\/p>\n<p><code>plt.xlabel('Time (seconds)')<\/code><\/p>\n<p><code>plt.ylabel('Heart Rate (bpm)')<\/code><\/p>\n<p><code>plt.title('Heart Rate Over Time')<\/code><\/p>\n<p><code>plt.legend()<\/code><\/p>\n<p><code>plt.grid(True)<\/code><\/p>\n<p><code>plt.show()<\/code><\/p>\n<p><code> <\/code><\/p>\n<p>The result? A processed dataset and a beautifully rendered graph showcasing the rhythm of my heart.<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/09c\/98e\/2b0\/09c98e2b00db0ac1089cd924a1382a11.png\" width=\"900\" height=\"590\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/09c\/98e\/2b0\/09c98e2b00db0ac1089cd924a1382a11.png\"\/><\/figure>\n<h4>Some Feature Specifics<\/h4>\n<p>In addition to specifics in working with data, especially external, and the need for workarounds to save data outside the current workbook, I found some other features.<\/p>\n<p>There is no need to import libraries, if those (libraries) are on dedicated for the job Microsoft servers. Note &#8212; in the `pd.to_numeric(rr_intervals[&#8216;rr_intervals&#8217;])` line, Pandas&#8217; `to_numeric` is called, but Pandas is not imported. The same is true for calling `plt`.<\/p>\n<p>Autocomplete works, but not as nicely as we&#8217;re used to.<\/p>\n<p>I&#8217;m sure the list of specifics is much longer.<\/p>\n<h4>Will I Use the Feature?<\/h4>\n<p>Reflecting on this journey, does the Python in Excel feature captivate me? The answer is more &#171;yes&#187; than &#171;no&#187;. For simple tasks, like basic processing of a small dataset, it&#8217;s a perfectly workable tool. But as soon as the task gets a little more complicated, regular Python (Anaconda in my case) is much more convenient and efficient.<\/p>\n<p>What about you? Will you try it?<\/p>\n<h4>Links:<\/h4>\n<ul>\n<li>\n<p><a href=\"https:\/\/support.microsoft.com\/en-us\/office\/introduction-to-python-in-excel-55643c2e-ff56-4168-b1ce-9428c8308545\" rel=\"noopener noreferrer nofollow\">Introduction      to Python in Excel<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"https:\/\/learn.microsoft.com\/en-us\/training\/modules\/intro-to-python\/\" rel=\"noopener noreferrer nofollow\">Introduction to Python &#8212; Training | Microsoft Learn\u00a0<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"https:\/\/support.microsoft.com\/office\/troubleshoot-python-in-excel-errors-7736520d-47ef-43a8-b640-d826afb63249\" rel=\"noopener noreferrer nofollow\">Troubleshoot Python in Excel errors<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"https:\/\/support.microsoft.com\/office\/data-security-and-python-in-excel-33cc88a4-4a87-485e-9ff9-f35958278327\" rel=\"noopener noreferrer nofollow\">Data security and Python in Excel<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"https:\/\/support.microsoft.com\/office\/python-in-excel-dataframes-a10495b2-8372-4f0f-9179-32771fe0dc04\" rel=\"noopener noreferrer nofollow\">Python in Excel DataFrames<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"https:\/\/support.microsoft.com\/office\/create-python-in-excel-plots-and-charts-798b5e8d-ce45-4451-8da3-f269cdad5cff\" rel=\"noopener noreferrer nofollow\">Create Python in Excel plots and charts<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"https:\/\/support.microsoft.com\/office\/use-power-query-data-with-python-in-excel-028dbcd4-76c5-4aa4-831d-0e211fefc0a2\" rel=\"noopener noreferrer nofollow\">Use Power Query data with Python in Excel<\/a><\/p>\n<\/li>\n<\/ul>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><\/p>\n<div class=\"tm-article-poll-container\"><!--[--><\/p>\n<div class=\"tm-article-poll tm-article-poll_variant-bordered\">\n<div class=\"tm-notice tm-notice_positive tm-article-poll__notice\"><!----><\/p>\n<div class=\"tm-notice__inner\"><!----><\/p>\n<div class=\"tm-notice__content\" data-test-id=\"notice-content\"><!--[--><span>\u0422\u043e\u043b\u044c\u043a\u043e \u0437\u0430\u0440\u0435\u0433\u0438\u0441\u0442\u0440\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0435 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0438 \u043c\u043e\u0433\u0443\u0442 \u0443\u0447\u0430\u0441\u0442\u0432\u043e\u0432\u0430\u0442\u044c \u0432 \u043e\u043f\u0440\u043e\u0441\u0435. <a rel=\"nofollow\" href=\"\/kek\/v1\/auth\/habrahabr\/?back=\/ru\/articles\/770006\/&#038;hl=ru\">\u0412\u043e\u0439\u0434\u0438\u0442\u0435<\/a>, \u043f\u043e\u0436\u0430\u043b\u0443\u0439\u0441\u0442\u0430.<\/span><!--]--><\/div>\n<\/div>\n<\/div>\n<p><!--[--><\/p>\n<div class=\"tm-article-poll__header\">Will You Use Python in Excel?<\/div>\n<div class=\"tm-article-poll__answers\"><!--[--><\/p>\n<div class=\"tm-article-poll__answer\">\n<div class=\"tm-article-poll__answer-data\"><span class=\"tm-article-poll__answer-percent\">0% <\/span><span class=\"tm-article-poll__answer-label\">Sure thing!<\/span><span class=\"tm-article-poll__answer-votes\">0<\/span><\/div>\n<div class=\"tm-article-poll__answer-bar\">\n<div class=\"tm-article-poll__answer-progress\" style=\"width: 0%\"><\/div>\n<\/div>\n<\/div>\n<div class=\"tm-article-poll__answer\">\n<div class=\"tm-article-poll__answer-data\"><span class=\"tm-article-poll__answer-percent\">0% <\/span><span class=\"tm-article-poll__answer-label\">No, I don&#8217;t think it&#8217;s useful.<\/span><span class=\"tm-article-poll__answer-votes\">0<\/span><\/div>\n<div class=\"tm-article-poll__answer-bar\">\n<div class=\"tm-article-poll__answer-progress\" style=\"width: 0%\"><\/div>\n<\/div>\n<\/div>\n<div class=\"tm-article-poll__answer\">\n<div class=\"tm-article-poll__answer-data\"><span class=\"tm-article-poll__answer-percent\">50% <\/span><span class=\"tm-article-poll__answer-label\">I&#8217;ll give it a try<\/span><span class=\"tm-article-poll__answer-votes\">1<\/span><\/div>\n<div class=\"tm-article-poll__answer-bar\">\n<div class=\"tm-article-poll__answer-progress\" style=\"width: 50%\"><\/div>\n<\/div>\n<\/div>\n<div class=\"tm-article-poll__answer\">\n<div class=\"tm-article-poll__answer-data\"><span class=\"tm-article-poll__answer-percent\">0% <\/span><span class=\"tm-article-poll__answer-label\">What&#8217;s Python?<\/span><span class=\"tm-article-poll__answer-votes\">0<\/span><\/div>\n<div class=\"tm-article-poll__answer-bar\">\n<div class=\"tm-article-poll__answer-progress\" style=\"width: 0%\"><\/div>\n<\/div>\n<\/div>\n<div class=\"tm-article-poll__answer\">\n<div class=\"tm-article-poll__answer-data\"><span class=\"tm-article-poll__answer-percent tm-article-poll__answer-percent_winning\">50% <\/span><span class=\"tm-article-poll__answer-label\">What&#8217;s Excel?<\/span><span class=\"tm-article-poll__answer-votes\">1<\/span><\/div>\n<div class=\"tm-article-poll__answer-bar\">\n<div class=\"tm-article-poll__answer-progress tm-article-poll__answer-progress_winning\" style=\"width: 50%\"><\/div>\n<\/div>\n<\/div>\n<p><!--]--><\/div>\n<div class=\"tm-article-poll__stats\"> \u041f\u0440\u043e\u0433\u043e\u043b\u043e\u0441\u043e\u0432\u0430\u043b\u0438 2 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f.    \u0412\u043e\u0437\u0434\u0435\u0440\u0436\u0430\u0432\u0448\u0438\u0445\u0441\u044f \u043d\u0435\u0442. <\/div>\n<p><!--]--><\/div>\n<p><!--]--><\/div>\n<p> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/770006\/\"> https:\/\/habr.com\/ru\/articles\/770006\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>There was already news on Habr about this significant event. Indeed, it resembles a retelling of the official Microsoft press release, but that&#8217;s how the &#8216;news&#8217; should be.<\/p>\n<figure class=\"full-width\"><\/figure>\n<h3>My Early Attempts<\/h3>\n<p>For some reason that even I don&#8217;t understand, I started trying to use Python inside Excel a few weeks ago. Unsuccessfully. Despite all my efforts, it didn&#8217;t work: initially, I connected to Microsoft 365 Insider, Beta Channel and didn&#8217;t observe the option there. Then I downloaded an add-in from Excel Labs, the Microsoft Garage project. The feature, although it started, did not work for me, constantly returning errors.<\/p>\n<h3>The Big Update<\/h3>\n<p>And then, on 12.10.2023, I received an email with good news: after updating Office, there is now a &#171;Python in Excel&#187; option in the Formulas menu. Entering &#171;=PY(&#187; in the command line converts the current cell into something resembling a Jupyter Notebook cell.<\/p>\n<p><strong><em>Important Reminder:<\/em><\/strong><em> \u00a0To access and use the &#171;Python in Excel&#187; feature, you need to be part of the &#171;Microsoft 365 Insider, Beta Channel.&#187;<\/em><\/p>\n<figure class=\"full-width\"><\/figure>\n<h3>How Does It Work?<\/h3>\n<p>The code is executed on Microsoft servers. All you need to do on your local computer is start Excel and enter the familiar magical combination &#171;=PY(&#171;. Then you can enter the code. Of course, copy-paste works. The syntax is Python, with a slight specificity if you need to access data. The &#8216;Ctrl+Enter&#8217; command ignites the code.<\/p>\n<figure class=\"full-width\"><\/figure>\n<h3>The Big Question: Why?<\/h3>\n<p>Why do we need Python inside Excel at all? This is the main question, a huge \u201cWhy?\u201d Are there any advantages over using the two products independently and exchanging data between them when necessary? Possibly there are, but with a big question mark. And despite all my heavy pondering, I can&#8217;t find an answer, although I&#8217;m trying very hard.<\/p>\n<h3>Potential Advantages<\/h3>\n<p>&#8212; You can avoid installing Python altogether.<\/p>\n<p>&#8212; For simple tasks that are more efficiently solved by code than by built-in Excel tools, you don&#8217;t need to multiply entities: both code and data can live inside one file.<\/p>\n<p>&#8212; Another reason to utilize this feature is as a substitute for Microsoft Excel macros, especially if you&#8217;re well-acquainted with Python but not with macros. Thanks to <a href=\"https:\/\/habr.com\/ru\/users\/alexandershelupinin\/\" rel=\"noopener noreferrer nofollow\">@alexandershelupinin<\/a> who <a href=\"https:\/\/habr.com\/ru\/articles\/769732\/#comment_26091220\" rel=\"noopener noreferrer nofollow\">pointed<\/a> this out.<\/p>\n<p>&#8212; And perhaps something else that I&#8217;m not aware of or haven&#8217;t thought of.<\/p>\n<h3>My Personal Take<\/h3>\n<p>Thinking soberly, I can&#8217;t come up with anything rational and sensible, which would require forgetting Python in Excel as an unnecessary toy. But! Procrastination and\/or curiosity force me to figure out the feature and come up with scenarios for using it. A man is weak (I definitely am), can&#8217;t resist temptations. The result of my weakness is this material.<\/p>\n<h4>First Impressions<\/h4>\n<p>According to my disgusting habit, instead of starting the process by carefully reading relevant documentation, I started exploring the feature by using the simplest and most effective of all methods, namely the &#171;trial and error method&#187;. Which, as often happens, led to several hours of bouncing from one elementary mistake to another and a waste of time and nervous energy. Common sense prevailed and &#8212; wonder of wonders! &#8212; after reading the documentation (see links below), the mystery was solved.\u00a0\u00a0\u00a0\u00a0\u00a0 <\/p>\n<p>As it turned out, the fact that the code runs on Microsoft&#8217;s servers has side effects: time delay, can&#8217;t install third party libraries, accessing data outside the current workbook only via Power Query, can&#8217;t save data outside the current workbook, etc.<\/p>\n<figure class=\"full-width\"><\/figure>\n<p>Plus, Python syntax is often with adjustments for &#171;living inside Excel&#187;. For example, to assign the value from cell B2 to a variable, we write x = xl(&#171;B2&#187;). And of course, writing code inside Excel&#8217;s formula bar is a pleasure for perverts. Although there is nothing stopping you from writing in a text editor and then copy-pasting. With all the big and small &#171;BUTs&#187;.<\/p>\n<p>Let me try it on a real-life use case.<\/p>\n<h4>Epic Fail<\/h4>\n<p>Since mid-January this year, 2023, I&#8217;ve been having a peculiar romance with Garmin, specifically its sports watches, mobile app, and personal account. This wasn&#8217;t your everyday romance; it was a dance of numbers and data. Every week, like clockwork, I would download and process various sets of data\u2014workouts, breathing exercises, and more. Then, I would merge these with records of my resting heart rate and HRV. My trusty Python script managed all this seamlessly. But one day, in a mix of curiosity and perhaps a dash of procrastination, I wondered: Could Excel&#8217;s new Python feature handle this?<\/p>\n<p>With optimism, I dove in. I imagined my CSV file, brimming with raw data from the Activities section of my Garmin account, being effortlessly tamed by this new Excel Python integration. I dreamt of easy summarizations, data pulls, merges, and updates. But reality had other plans. After several grueling hours, I had to admit defeat and put this challenge aside.<\/p>\n<h3>And Now It Worked<\/h3>\n<h4>Working with CSV: reading and processing data<\/h4>\n<p>As part of my \u201cromantic\u201d routine I need to supplement my Garmin data with heart rate readings from the Elite HRV app. The non-PRO version of Elite HRV doesn&#8217;t allow for direct data exports, but with a simple request, they send you a link to download an archive of TXT files. These files contain RR intervals\u2014each representing the milliseconds between heartbeats. A cardiologist may task you with extracting heart rate readings from these intervals and plotting a graph to show heart rate fluctuations over time.<\/p>\n<p>With renewed determination, I tackled the challenge using Python in Excel. There were a few hiccups, especially with the query creation, but in the end, it took me just about fifteen minutes to draft a rather intricate code.<\/p>\n<p><code># read data<\/code><\/p>\n<p><code>rr_intervals = xl(\"EliteHRV_SS\")<\/code><\/p>\n<p><code>rr_intervals.columns = ['rr_intervals']<\/code><\/p>\n<p><code>rr_intervals['rr_intervals']\u00a0 = pd.to_numeric(rr_intervals['rr_intervals'])<\/code><\/p>\n<p><code># calculating heart rate for each interval<\/code><\/p>\n<p><code>rr_intervals['heart_rate'] = (60000 \/ rr_intervals['rr_intervals']).astype(int)\u00a0<\/code><\/p>\n<p><code># Calculate cumulative time in seconds for each RR interval<\/code><\/p>\n<p><code>cumulative_time = np.cumsum(rr_intervals['rr_intervals'].values) \/ 1000\u00a0 # Convert from ms to s<\/code><\/p>\n<p><code>cumulative_time = [int(t) for t in cumulative_time]\u00a0<\/code><\/p>\n<p><code>heart_rates = rr_intervals['heart_rate'].values\u00a0<\/code><\/p>\n<p><code>df = pd.DataFrame({<\/code><\/p>\n<p><code>\u00a0\u00a0\u00a0 'timepoints': cumulative_time,<\/code><\/p>\n<p><code>\u00a0\u00a0\u00a0 'heart_rate': heart_rates<\/code><\/p>\n<p><code>\u00a0\u00a0\u00a0 })<\/code><\/p>\n<p><code># Plotting<\/code><\/p>\n<p><code>plt.figure(figsize=(15, 6))<\/code><\/p>\n<p><code>plt.plot(cumulative_time, heart_rates, label='Heart Rate', color='blue')<\/code><\/p>\n<p><code>plt.xlabel('Time (seconds)')<\/code><\/p>\n<p><code>plt.ylabel('Heart Rate (bpm)')<\/code><\/p>\n<p><code>plt.title('Heart Rate Over Time')<\/code><\/p>\n<p><code>plt.legend()<\/code><\/p>\n<p><code>plt.grid(True)<\/code><\/p>\n<p><code>plt.show()<\/code><\/p>\n<p><code> <\/code><\/p>\n<p>The result? A processed dataset and a beautifully rendered graph showcasing the rhythm of my heart.<\/p>\n<figure class=\"full-width\"><\/figure>\n<h4>Some Feature Specifics<\/h4>\n<p>In addition to specifics in working with data, especially external, and the need for workarounds to save data outside the current workbook, I found some other features.<\/p>\n<p>There is no need to import libraries, if those (libraries) are on dedicated for the job Microsoft servers. Note &#8212; in the `pd.to_numeric(rr_intervals[&#8216;rr_intervals&#8217;])` line, Pandas&#8217; `to_numeric` is called, but Pandas is not imported. The same is true for calling `plt`.<\/p>\n<p>Autocomplete works, but not as nicely as we&#8217;re used to.<\/p>\n<p>I&#8217;m sure the list of specifics is much longer.<\/p>\n<h4>Will I Use the Feature?<\/h4>\n<p>Reflecting on this journey, does the Python in Excel feature captivate me? The answer is more &#171;yes&#187; than &#171;no&#187;. For simple tasks, like basic processing of a small dataset, it&#8217;s a perfectly workable tool. But as soon as the task gets a little more complicated, regular Python (Anaconda in my case) is much more convenient and efficient.<\/p>\n<p>What about you? Will you try it?<\/p>\n<h4>Links:<\/h4>\n<ul>\n<li>\n<p><a href=\"https:\/\/support.microsoft.com\/en-us\/office\/introduction-to-python-in-excel-55643c2e-ff56-4168-b1ce-9428c8308545\" rel=\"noopener noreferrer nofollow\">Introduction      to Python in Excel<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"https:\/\/learn.microsoft.com\/en-us\/training\/modules\/intro-to-python\/\" rel=\"noopener noreferrer nofollow\">Introduction to Python &#8212; Training | Microsoft Learn\u00a0<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"https:\/\/support.microsoft.com\/office\/troubleshoot-python-in-excel-errors-7736520d-47ef-43a8-b640-d826afb63249\" rel=\"noopener noreferrer nofollow\">Troubleshoot Python in Excel errors<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"https:\/\/support.microsoft.com\/office\/data-security-and-python-in-excel-33cc88a4-4a87-485e-9ff9-f35958278327\" rel=\"noopener noreferrer nofollow\">Data security and Python in Excel<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"https:\/\/support.microsoft.com\/office\/python-in-excel-dataframes-a10495b2-8372-4f0f-9179-32771fe0dc04\" rel=\"noopener noreferrer nofollow\">Python in Excel DataFrames<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"https:\/\/support.microsoft.com\/office\/create-python-in-excel-plots-and-charts-798b5e8d-ce45-4451-8da3-f269cdad5cff\" rel=\"noopener noreferrer nofollow\">Create Python in Excel plots and charts<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"https:\/\/support.microsoft.com\/office\/use-power-query-data-with-python-in-excel-028dbcd4-76c5-4aa4-831d-0e211fefc0a2\" rel=\"noopener noreferrer nofollow\">Use Power Query data with Python in Excel<\/a><\/p>\n<\/li>\n<\/ul>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><\/p>\n<div class=\"tm-article-poll-container\"><!--[--><\/p>\n<div class=\"tm-article-poll tm-article-poll_variant-bordered\">\n<div class=\"tm-notice tm-notice_positive tm-article-poll__notice\"><!----><\/p>\n<div class=\"tm-notice__inner\"><!----><\/p>\n<div class=\"tm-notice__content\" data-test-id=\"notice-content\"><!--[--><span>\u0422\u043e\u043b\u044c\u043a\u043e \u0437\u0430\u0440\u0435\u0433\u0438\u0441\u0442\u0440\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0435 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0438 \u043c\u043e\u0433\u0443\u0442 \u0443\u0447\u0430\u0441\u0442\u0432\u043e\u0432\u0430\u0442\u044c \u0432 \u043e\u043f\u0440\u043e\u0441\u0435. <a rel=\"nofollow\" href=\"\/kek\/v1\/auth\/habrahabr\/?back=\/ru\/articles\/770006\/&#038;hl=ru\">\u0412\u043e\u0439\u0434\u0438\u0442\u0435<\/a>, \u043f\u043e\u0436\u0430\u043b\u0443\u0439\u0441\u0442\u0430.<\/span><!--]--><\/div>\n<\/div>\n<\/div>\n<p><!--[--><\/p>\n<div class=\"tm-article-poll__header\">Will You Use Python in Excel?<\/div>\n<div class=\"tm-article-poll__answers\"><!--[--><\/p>\n<div class=\"tm-article-poll__answer\">\n<div class=\"tm-article-poll__answer-data\"><span class=\"tm-article-poll__answer-percent\">0% <\/span><span class=\"tm-article-poll__answer-label\">Sure thing!<\/span><span class=\"tm-article-poll__answer-votes\">0<\/span><\/div>\n<div class=\"tm-article-poll__answer-bar\">\n<div class=\"tm-article-poll__answer-progress\" style=\"width: 0%\"><\/div>\n<\/div>\n<\/div>\n<div class=\"tm-article-poll__answer\">\n<div class=\"tm-article-poll__answer-data\"><span class=\"tm-article-poll__answer-percent\">0% <\/span><span class=\"tm-article-poll__answer-label\">No, I don&#8217;t think it&#8217;s useful.<\/span><span class=\"tm-article-poll__answer-votes\">0<\/span><\/div>\n<div class=\"tm-article-poll__answer-bar\">\n<div class=\"tm-article-poll__answer-progress\" style=\"width: 0%\"><\/div>\n<\/div>\n<\/div>\n<div class=\"tm-article-poll__answer\">\n<div class=\"tm-article-poll__answer-data\"><span class=\"tm-article-poll__answer-percent\">50% <\/span><span class=\"tm-article-poll__answer-label\">I&#8217;ll give it a try<\/span><span class=\"tm-article-poll__answer-votes\">1<\/span><\/div>\n<div class=\"tm-article-poll__answer-bar\">\n<div class=\"tm-article-poll__answer-progress\" style=\"width: 50%\"><\/div>\n<\/div>\n<\/div>\n<div class=\"tm-article-poll__answer\">\n<div class=\"tm-article-poll__answer-data\"><span class=\"tm-article-poll__answer-percent\">0% <\/span><span class=\"tm-article-poll__answer-label\">What&#8217;s Python?<\/span><span class=\"tm-article-poll__answer-votes\">0<\/span><\/div>\n<div class=\"tm-article-poll__answer-bar\">\n<div class=\"tm-article-poll__answer-progress\" style=\"width: 0%\"><\/div>\n<\/div>\n<\/div>\n<div class=\"tm-article-poll__answer\">\n<div class=\"tm-article-poll__answer-data\"><span class=\"tm-article-poll__answer-percent tm-article-poll__answer-percent_winning\">50% <\/span><span class=\"tm-article-poll__answer-label\">What&#8217;s Excel?<\/span><span class=\"tm-article-poll__answer-votes\">1<\/span><\/div>\n<div class=\"tm-article-poll__answer-bar\">\n<div class=\"tm-article-poll__answer-progress tm-article-poll__answer-progress_winning\" style=\"width: 50%\"><\/div>\n<\/div>\n<\/div>\n<p><!--]--><\/div>\n<div class=\"tm-article-poll__stats\"> \u041f\u0440\u043e\u0433\u043e\u043b\u043e\u0441\u043e\u0432\u0430\u043b\u0438 2 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f.    \u0412\u043e\u0437\u0434\u0435\u0440\u0436\u0430\u0432\u0448\u0438\u0445\u0441\u044f \u043d\u0435\u0442. <\/div>\n<p><!--]--><\/div>\n<p><!--]--><\/div>\n<p> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/770006\/\"> https:\/\/habr.com\/ru\/articles\/770006\/<\/a><br \/><\/br><\/br><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-358552","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/358552","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=358552"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/358552\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=358552"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=358552"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=358552"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}