この記事では、pandas と xlsxwriter を使用して Python で xlsx ファイルを読み書きする方法 (コード付き) を紹介します。これには一定の参考価値があります。必要な友人は参照できます。お役に立てば幸いです。手伝う。
既存の xlsx ファイルは次のとおりです:
1. 最初の n 行のすべてのデータを読み取ります
# coding: utf-8 import pandas as pd # 1. 读取前n行所有数据 df = pd.read_excel('school.xlsx')#读取xlsx中第一个sheet data1 = df.head(7) # 读取前7行的所有数据,dataFrame结构 data2 = df.values #list形式,读取表格所有数据 print("获取到所有的值:\n{0}".format(data1)) #格式化输出 print("获取到所有的值:\n{0}".format(data2)) #格式化输出
2. 特定のデータを読み取ります行、特定の列
# coding: utf-8 import pandas as pd # 2. 读取特定行,特定列 df = pd.read_excel('school.xlsx') #读取xlsx中第一个sheet data1 = df.ix[0].values #读取第一行所有数据,0表示第一行,不包含表头 data2 = df.ix[1,1] #读取指定行列位置数据 data3 = df.ix[[1,2]].values #读取指定多行 data4 = df.ix[:,[0]].values #读取指定列的所有行 #data4 = df[u'class'].values #同上 data5 = df.ix[:,[u'class',u'name']].values #读取指定键值列的所有行 print("数据:\n{0}".format(data1)) print("数据:\n{0}".format(data2)) print("数据:\n{0}".format(data3)) print("数据:\n{0}".format(data4)) print("数据:\n{0}".format(data5))
3. xlsxファイルの行番号、すべての列名を取得
# coding: utf-8 import pandas as pd # 3. 获取xlsx文件行号,所有列名称 df = pd.read_excel('school.xlsx') #读取xlsx中第一个sheet print("输出行号列表{}".format(df.index.values)) # 获取xlsx文件的所有行号 print("输出列标题{}".format(df.columns.values)) #所有列名称
4. xlsxデータを読み取り、辞書に変換
# coding: utf-8 import pandas as pd # 4. 读取xlsx数据转换为字典 df = pd.read_excel('school.xlsx') #读取xlsx中第一个sheet test_data=[] for i in df.index.values:#获取行号的索引,并对其进行遍历: #根据i来获取每一行指定的数据 并利用to_dict转成字典 row_data=df.ix[i,['id','name','class','data','stature']].to_dict() test_data.append(row_data) print("最终获取到的数据是:{0}".format(test_data))
5. xlsxファイルを書き込みます
#coding: utf-8 import xlsxwriter # 创建工作簿 file_name = "first_book.xlsx" workbook = xlsxwriter.Workbook(file_name) # 创建工作表 worksheet = workbook.add_worksheet('sheet1') # 写单元格 worksheet.write(0, 0, 'id') worksheet.write(0,1, 'name') worksheet.write(0,2, 'class') worksheet.write(0,3, 'data') # 写行 worksheet.write_row(1, 0, [1, 2, 3]) # 写列,其中列D需要大写 worksheet.write_column('D2', ['a', 'b', 'c']) # 关闭工作簿 workbook.close()
記述される xlsx ファイルは次のとおりです:
以上がPythonでpandasとxlsxwriterを使ってxlsxファイルを読み書きする方法の紹介(コード付き)の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

Arraysinpython、特にvianumpy、arecrucialinscientificComputing fortheirefficienty andversitility.1)彼らは、fornumericaloperations、data analysis、andmachinelearning.2)numpy'simplementation incensuresfasteroperationsthanpasteroperations.3)arayableminablecickick

Pyenv、Venv、およびAnacondaを使用して、さまざまなPythonバージョンを管理できます。 1)Pyenvを使用して、複数のPythonバージョンを管理します。Pyenvをインストールし、グローバルバージョンとローカルバージョンを設定します。 2)VENVを使用して仮想環境を作成して、プロジェクトの依存関係を分離します。 3)Anacondaを使用して、データサイエンスプロジェクトでPythonバージョンを管理します。 4)システムレベルのタスク用にシステムPythonを保持します。これらのツールと戦略を通じて、Pythonのさまざまなバージョンを効果的に管理して、プロジェクトのスムーズな実行を確保できます。

numpyarrayshaveveraladvantages-averstandardpythonarrays:1)thealmuchfasterduetocベースのインプレンテーション、2)アレモレメモリ効率、特にlargedatasets、および3)それらは、拡散化された、構造化された形成術科療法、

パフォーマンスに対する配列の均一性の影響は二重です。1)均一性により、コンパイラはメモリアクセスを最適化し、パフォーマンスを改善できます。 2)しかし、タイプの多様性を制限し、それが非効率につながる可能性があります。要するに、適切なデータ構造を選択することが重要です。

craftexecutablepythonscripts、次のようになります

numpyarraysarasarebetterfornumeroperations andmulti-dimensionaldata、whilethearraymoduleissuitable forbasic、1)numpyexcelsinperformance and forlargedatasentassandcomplexoperations.2)thearraymuremememory-effictientivearientfa

NumPyArraySareBetterforHeavyNumericalComputing、whilethearrayarayismoreSuitableformemory-constrainedprojectswithsimpledatatypes.1)numpyarraysofferarays andatiledance andpeperancedatasandatassandcomplexoperations.2)thearraymoduleisuleiseightweightandmemememe-ef

ctypesallowsinging andmanipulatingc-stylearraysinpython.1)usectypestointerfacewithclibrariesforperformance.2)createc-stylearraysfornumericalcomputations.3)passarraystocfunctions foreffientientoperations.how、how、becuutiousmorymanagemation、performanceo


ホットAIツール

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

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

Undress AI Tool
脱衣画像を無料で

Clothoff.io
AI衣類リムーバー

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

人気の記事

ホットツール

SAP NetWeaver Server Adapter for Eclipse
Eclipse を SAP NetWeaver アプリケーション サーバーと統合します。

DVWA
Damn Vulnerable Web App (DVWA) は、非常に脆弱な PHP/MySQL Web アプリケーションです。その主な目的は、セキュリティ専門家が法的環境でスキルとツールをテストするのに役立ち、Web 開発者が Web アプリケーションを保護するプロセスをより深く理解できるようにし、教師/生徒が教室環境で Web アプリケーションを教え/学習できるようにすることです。安全。 DVWA の目標は、シンプルでわかりやすいインターフェイスを通じて、さまざまな難易度で最も一般的な Web 脆弱性のいくつかを実践することです。このソフトウェアは、

EditPlus 中国語クラック版
サイズが小さく、構文の強調表示、コード プロンプト機能はサポートされていません

PhpStorm Mac バージョン
最新(2018.2.1)のプロフェッショナル向けPHP統合開発ツール

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

ホットトピック









