{"id":834,"date":"2024-02-13T20:44:26","date_gmt":"2024-02-13T09:44:26","guid":{"rendered":"https:\/\/tobyhuang.com\/?p=834"},"modified":"2025-12-11T18:53:08","modified_gmt":"2025-12-11T07:53:08","slug":"using-python-deal-with-battery-cycle-data","status":"publish","type":"post","link":"https:\/\/tobyhuang.com\/index.php\/2024\/02\/13\/using-python-deal-with-battery-cycle-data\/","title":{"rendered":"Using Python to deal with battery cycle data"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">When we got the battery cycle data, we had millions or even billions of data points of charge-discharge capacity-voltage curve. If we have time, we may use the tester software to manually select the data curves we want.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">What should we do if we get the raw data of all cycle data?<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Let&#8217;s see an example using Python and Tableau to deal with the data.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Now we get raw data of a battery cycle performance test. It runs about 100+ cycles, and 40 thousand raws \u00d7 2 clomuns.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">First, import the libraries we will use in Python.<\/p>\n\n\n\n<pre class=\"wp-block-code has-luminous-vivid-orange-color has-text-color has-link-color has-medium-font-size wp-elements-d23eac05b1e902b84d85dc0edf245609\"><code>import pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport seaborn as sns<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Then, load the data we get.<\/p>\n\n\n\n<pre class=\"wp-block-code has-luminous-vivid-orange-color has-text-color has-medium-font-size\"><code><code>df = pd.read_excel(<span style=\"color: #ff0000;\">'your file path\/file name.xlsx'<\/span>)<\/code><\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Let us check the data.<\/p>\n\n\n\n<pre class=\"wp-block-code has-luminous-vivid-orange-color has-text-color has-medium-font-size\"><code>df.shape\ndf.head()<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">we will get:(40216, 3) and first 6 rows of the excel, which means 40216 rows and 3 columns,&nbsp; and the column is &#8216;index&#8217;, &#8216;capcity\/mAh\/g&#8217; and &#8216;voltage\/V&#8217;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Let&#8217;s slice the data. Give the data cycle number and charge or discharge status. This will help us draw figures in Tableau.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">How to slice the data? We first ask the tester the logics of the charge-discharge process. They use constant current to charge and discharge the battery. When voltage lower than 0.8 V, the process goes to charge, and when voltage larger than 2.8 V, the process goes to discharge. The process begins with charge, end until cycle numbers larger than input (manually set).<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Then we can give the data cycle numbers and charge-discharge status. The cycle number and status are used in Tableau to color or filter the data or as the axis for the figure.<\/p>\n\n\n\n<pre class=\"wp-block-code has-luminous-vivid-orange-color has-text-color has-link-color has-medium-font-size wp-elements-26b6fd12f288cac0cbe3b7caa39c2780\"><code>cycle = 1\nfor i in range(len(df&#91;'Voltage\/V'])):\n    df.loc&#91;i,'cycle'] = cycle\n    df.loc&#91;0,'status'] = 'charge'\n        \n    if (df.loc&#91;i,'status'] == 'charge') &amp; (df.loc&#91;i,'Voltage\/V'] &lt;= 2.8):\n        df.loc&#91;i+1,'status'] = 'charge'\n        \n    elif (df.loc&#91;i,'status'] == 'charge') &amp; (df.loc&#91;i,'Voltage\/V'] &gt; 2.8):\n        df.loc&#91;i+1,'status'] = 'discharge'\n        \n    elif (df.loc&#91;i,'status'] == 'discharge') &amp; (df.loc&#91;i,'Voltage\/V'] &gt;= 0.8):\n        df.loc&#91;i+1,'status'] = 'discharge'\n\n    elif (df.loc&#91;i,'status'] == 'discharge') &amp; (df.loc&#91;i,'Voltage\/V'] &lt; 0.8):\n        cycle += 1\n        df.loc&#91;i+1,'status'] = 'charge'\n        \n    \ndf.dropna(inplace=True)\ndf.to_csv('your path\/after data.csv')<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Now each row of the data was given the cycle number and charge-discharge status. They are easily import to Tableau and be shown.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Wow, let&#8217;s see what we can get. All the charge-discharge capacity VS voltage, Capacity VS Cycle numbers. For capacity VS voltage figure, we set capacity as column and voltage as arrow, draw cycle to color. For Capacity VS Cycle numbers, we set cycle as column and capacity as arrow and let column number is maximum number, and draw status to color to divid the charge capacity and discharge capacity.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">(From <a href=\"https:\/\/www.linkedin.com\/in\/toby-huang-083b441a4\/?lipi=urn%3Ali%3Apage%3Ad_flagship3_feed%3BfJqITqNGTXmKc%2BOLEv30ag%3D%3D\" data-type=\"link\" data-id=\"https:\/\/www.linkedin.com\/in\/toby-huang-083b441a4\/?lipi=urn%3Ali%3Apage%3Ad_flagship3_feed%3BfJqITqNGTXmKc%2BOLEv30ag%3D%3D\" target=\"_blank\" rel=\"noreferrer noopener\">Toby<\/a> via Linkedin.)<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"820\" src=\"https:\/\/tobyhuang.com\/wp-content\/uploads\/2024\/02\/\u622a\u5c4f2024-02-13-\u4e0b\u53488.32.32-1024x820.png\" alt=\"\" class=\"wp-image-847\" srcset=\"https:\/\/tobyhuang.com\/wp-content\/uploads\/2024\/02\/\u622a\u5c4f2024-02-13-\u4e0b\u53488.32.32-1024x820.png 1024w, https:\/\/tobyhuang.com\/wp-content\/uploads\/2024\/02\/\u622a\u5c4f2024-02-13-\u4e0b\u53488.32.32-300x240.png 300w, https:\/\/tobyhuang.com\/wp-content\/uploads\/2024\/02\/\u622a\u5c4f2024-02-13-\u4e0b\u53488.32.32-768x615.png 768w, https:\/\/tobyhuang.com\/wp-content\/uploads\/2024\/02\/\u622a\u5c4f2024-02-13-\u4e0b\u53488.32.32-1536x1230.png 1536w, https:\/\/tobyhuang.com\/wp-content\/uploads\/2024\/02\/\u622a\u5c4f2024-02-13-\u4e0b\u53488.32.32-1140x913.png 1140w, https:\/\/tobyhuang.com\/wp-content\/uploads\/2024\/02\/\u622a\u5c4f2024-02-13-\u4e0b\u53488.32.32.png 2010w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>When we got the battery cycle data, we had millions or even billions of data&hellip;<\/p>\n","protected":false},"author":1,"featured_media":847,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[32],"tags":[],"class_list":["post-834","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-code-sharing"],"_links":{"self":[{"href":"https:\/\/tobyhuang.com\/index.php\/wp-json\/wp\/v2\/posts\/834","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/tobyhuang.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/tobyhuang.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/tobyhuang.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/tobyhuang.com\/index.php\/wp-json\/wp\/v2\/comments?post=834"}],"version-history":[{"count":9,"href":"https:\/\/tobyhuang.com\/index.php\/wp-json\/wp\/v2\/posts\/834\/revisions"}],"predecessor-version":[{"id":883,"href":"https:\/\/tobyhuang.com\/index.php\/wp-json\/wp\/v2\/posts\/834\/revisions\/883"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/tobyhuang.com\/index.php\/wp-json\/wp\/v2\/media\/847"}],"wp:attachment":[{"href":"https:\/\/tobyhuang.com\/index.php\/wp-json\/wp\/v2\/media?parent=834"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tobyhuang.com\/index.php\/wp-json\/wp\/v2\/categories?post=834"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tobyhuang.com\/index.php\/wp-json\/wp\/v2\/tags?post=834"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}