27
25

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

[Python] pandasを使ってcsvファイルの読み込み

Last updated at Posted at 2017-08-09

Pandasについて

PandasはPythonのデータ分析用ライブラリです。
インストールは pip install pandas

csvファイルの読み込み

sample.py
import pandas as pd
xxx = pd.read_csv('sample.csv') #xxxは適当な変数

print (xxx) # 全カラムの出力

##サンプル
スクリーンショット 2017-08-09 16.36.54.png
sample.csv

sample.csv内で一番高い点数をとった学生番号を出力したい場合

sample2.py
import pandas as pd
filename = pd.read_csv('sample.csv',encoding="SHIFT-JIS")#もしくはUTF-8

listA = []
listB = []

listA =  filename['学生番号']
listB =  filename['点数']
max=0
leng = len(listA)

for n in range(leng):
	if listB[n]>max:
		max=listB[n]
		num = n
print ('最高得点をとった学生番号は'+str(listA[num]))

##まとめ
Pandasを使うと簡単にcsvなどのエクセルファイルを読み込めるので便利です。これからpythonを使ってデータ分析をしてみたいという方は使ってみてはいかがでしょうか。

27
25
3

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
27
25

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?