Pandas を使用して既存の Excel ファイルに新しいシートを追加する
Pandas は、Excel ファイルを操作するための便利なインターフェイスを提供します。ただし、新しいシートを既存のファイルに保存する場合、ユーザーは多くの場合、問題に遭遇します。この記事では、この問題に対処し、包括的な解決策を提供します。
既存のコード:
提供されたコードは 2 つの DataFrame を 2 つのシートに保存しますが、データフレームを失うことなくシートを追加できません。 ExcelWriter オブジェクトを閉じることにより、元のデータが失われます。
ベスト アプローチ:
理想的なワークフローには、Excel ファイルを作成し、データを保存し、ExcelWriter を閉じてから再度開くことが含まれます。さらにシートを追加します。このアプローチにより、データが確実に保存されます。
Openpyxl 統合:
データを失わずにシートを追加するために、Pandas は Openpyxl を利用できます。 Openpyxl を使用すると、Excel ファイルを直接操作できます。 load_workbook を使用して既存のファイルをロードし、それを ExcelWriter のブックに設定すると、既存のシートに影響を与えることなく新しいシートを追加できます。
完全な例:
これは完全な例:
<code class="python">import pandas as pd import numpy as np from openpyxl import load_workbook path = r"C:\Users\fedel\Desktop\excelData\PhD_data.xlsx" # Generate initial data x1 = np.random.randn(100, 2) df1 = pd.DataFrame(x1) x2 = np.random.randn(100, 2) df2 = pd.DataFrame(x2) # Write initial sheets writer = pd.ExcelWriter(path, engine='xlsxwriter') df1.to_excel(writer, sheet_name='x1') df2.to_excel(writer, sheet_name='x2') writer.close() # Open existing file and add new sheets book = load_workbook(path) writer = pd.ExcelWriter(path, engine='openpyxl') writer.book = book # Generate new data x3 = np.random.randn(100, 2) df3 = pd.DataFrame(x3) x4 = np.random.randn(100, 2) df4 = pd.DataFrame(x4) # Append new sheets df3.to_excel(writer, sheet_name='x3') df4.to_excel(writer, sheet_name='x4') writer.close()</code>
このコードは、最初に 2 つのシートを生成し、ExcelWriter を閉じます。次に、ファイルを再度開き、Openpyxl を利用してさらに 2 つのシートを追加し、元のデータを保持します。
以上がPandas を使用して既存の Excel ファイルに新しいシートを追加する方法の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

forhandlinglaredataSetsinpython、usenumpyArrays forbetterperformance.1)numpyarraysarememory-effictientandfasterfornumericaloperations.2)nusinnnnedarytypeconversions.3)レバレッジベクトル化は、測定済みのマネージメーシェイメージーウェイズデイタイです

inpython、listsusedynamicmemoryallocation with allocation、whilenumpyArraysalocatefixedmemory.1)listsallocatemorememorythanneededededinitivative.2)numpyArrayasallocateexactmemoryforements、rededicablebutlessflexibilityを提供します。

inpython、youcanspecthedatatypeyfelemeremodelernspant.1)usenpynernrump.1)usenpynerp.dloatp.ploatm64、フォーマーpreciscontrolatatypes。

numpyisessentialfornumericalcomputinginpythonduetoitsspeed、memory efficiency、andcomprehensivematicalfunctions.1)それは、performsoperations.2)numpyArraysaremoremory-efficientthanpythonlists.3)Itofderangeofmathematicaloperty

contiguousMemoryAllocationisucial forArraysは、ForeffienceAndfastelementAccess.1)iteenablesConstantTimeAccess、O(1)、DuetodirectAddresscalculation.2)itemprovesefficiencyByAllowingMultiblementFechesperCacheLine.3)itimplifieMememm

slicingapythonlistisdoneusingtheyntaxlist [start:stop:step] .hore'showitworks:1)startisthe indexofthefirstelementtoinclude.2)spotisthe indexofthefirmenttoeexclude.3)staptistheincrementbetbetinelements

numpyallows forvariousoperationsonarrays:1)basicarithmeticlikeaddition、減算、乗算、および分割; 2)AdvancedperationssuchasmatrixMultiplication;

Arraysinpython、特にnumpyandpandas、aresentialfordataanalysis、offeringspeedandeficiency.1)numpyarraysenable numpyarraysenable handling forlaredatasents andcomplexoperationslikemoverages.2)Pandasextendsnumpy'scapabivitieswithdataframesfortruc


ホットAIツール

Undresser.AI Undress
リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover
写真から衣服を削除するオンライン AI ツール。

Undress AI Tool
脱衣画像を無料で

Clothoff.io
AI衣類リムーバー

Video Face Swap
完全無料の AI 顔交換ツールを使用して、あらゆるビデオの顔を簡単に交換できます。

人気の記事

ホットツール

SublimeText3 英語版
推奨: Win バージョン、コードプロンプトをサポート!

ドリームウィーバー CS6
ビジュアル Web 開発ツール

SublimeText3 Mac版
神レベルのコード編集ソフト(SublimeText3)

SublimeText3 Linux 新バージョン
SublimeText3 Linux 最新バージョン

ZendStudio 13.5.1 Mac
強力な PHP 統合開発環境
