0
1

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 3 years have passed since last update.

pythonのpandasでcsv読み込む

Last updated at Posted at 2020-06-29

#pythonでcsvファイルを読み込む
・python
・sqlite3
・pandas

import sqlite3
import pandas as pd

df = pd.read_csv('{csvファイルのpath}', encoding='shift-jis') 
db_name = 'sample.db'  #dbの作成
conn = sqlite3.connect(db_name)  #dbへ接続
df.to_sql('class_table', conn, if_exists='replace')  #csvを取り込んだ情報をsqlへ変換
c = conn.cursor()
query = 'SELECT * FROM class_table'
result = c.execute(query)
c.fetchall()  #全件取得

for row in result:  #1件ずつ取得
  print(row)

for vert in range(2, 10):  #カラムを順に取得
  vert += 1
  for side in range(0, 4):
    print(df.loc[vert, side])
    side += 1

conn.close()  #db切断
0
1
0

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
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?