Использование панд для записи df в sqlite - PullRequest
0 голосов
/ 27 октября 2018

Я пытаюсь создать базу данных sqlite из файла CSV.После некоторого поиска кажется, что это возможно, используя pandas df.Я пытался следовать некоторым учебникам и документации, но я не могу понять эту ошибку.Вот мой код:

# Import libraries
import pandas, csv, sqlite3

# Create sqlite database and cursor
conn = sqlite3.connect('test.db')
c = conn.cursor()
# Create the table of pitches
c.execute("""CREATE TABLE IF NOT EXISTS pitches (
            pitch_type text,
            game_date text,
            release_speed real
            )""")

conn.commit()

df = pandas.read_csv('test2.csv')
df.to_sql('pitches', conn, if_exists='append', index=False)

conn.close()

Когда я запускаю этот код, я получаю следующую ошибку:

sqlite3.OperationalError: table pitches has no column named SL

SL - это первое значение в первой строке в моем файле CSV.Я не могу понять, почему он смотрит на значение csv как имя столбца, если только он не думает, что первая строка csv должна быть заголовками и пытается сопоставить это с именами столбцов в таблице?Я тоже не думаю, что это так, потому что я попытался изменить первое значение на фактическое имя столбца и получил ту же ошибку.

РЕДАКТИРОВАТЬ:

Когда у меня есть заголовки в CSV, фрейм данных выглядит следующим образом:

     pitch_type  game_date  release_speed
0            SL  8/31/2017           81.9
1            SL  8/31/2017           84.1
2            SL  8/31/2017           81.9
...         ...        ...            ...
2919         SL   8/1/2017           82.3
2920         CU   8/1/2017           78.7

[2921 rows x 3 columns]

, и я получаю следующую ошибку:

sqlite3.OperationalError: table pitches has no column named game_date

Когда я беру заголовки из файла csv:

      SL  8/31/2017  81.9
0     SL  8/31/2017  84.1
1     SL  8/31/2017  81.9
2     SL  8/31/2017  84.1
...   ..        ...   ...
2918  SL   8/1/2017  82.3
2919  CU   8/1/2017  78.7

[2920 rows x 3 columns]

и я получаю следующую ошибку:

sqlite3.OperationalError: table pitches has no column named SL

РЕДАКТИРОВАТЬ # 2:

Я пытался полностью исключить создание таблицы из кода, для этого ответа ,со следующим кодом:

# Import libraries
import pandas, csv, sqlite3

# Create sqlite database and cursor
conn = sqlite3.connect('test.db')
c = conn.cursor()

df = pandas.read_csv('test2.csv')
df.to_sql('pitches', conn, if_exists='append', index=False)

conn.close()

и все еще получаю ошибку

sqlite3.OperationalError: table pitches has no column named SL

РЕДАКТИРОВАТЬ # 3:

Я изменил код создания таблицына следующее:

# Create the table of pitches
dropTable = 'DROP TABLE pitches'
c.execute(dropTable)
createTable = "CREATE TABLE IF NOT EXISTS pitches(pitch_type text, game_date text, release_speed real)"
c.execute(createTable)

и это работает сейчас.Не уверен, что именно изменилось, так как для меня это выглядит в основном так же, но это работает.

Ответы [ 3 ]

0 голосов
/ 27 октября 2018

Если вы пытаетесь создать таблицу из CSV-файла, вы можете просто запустить sqlite3 и сделать:

sqlite> .mode csv
sqlite> .import c:/path/to/file/myfile.csv myTableName
0 голосов
/ 27 октября 2018

Проверьте названия столбцов. Я могу успешно воспроизвести ваш код без ошибок. Переменная names получает все имена столбцов из таблицы sqlite, и вы можете сравнить их с заголовками фрейма данных с помощью df.columns.

# Import libraries
import pandas as pd, csv, sqlite3

# Create sqlite database and cursor
conn = sqlite3.connect('test.db')
c = conn.cursor()
# Create the table of pitches
c.execute("""CREATE TABLE IF NOT EXISTS pitches (
            pitch_type text,
            game_date text,
            release_speed real
            )""")
conn.commit()

test = conn.execute('SELECT * from pitches')
names = [description[0] for description in test.description]
print(names)

df = pd.DataFrame([['SL','8/31/2017','81.9']],columns = ['pitch_type','game_date','release_speed'])
df.to_sql('pitches', conn, if_exists='append', index=False)

conn.execute('SELECT * from pitches').fetchall()
>> [('SL', '8/31/2017', 81.9), ('SL', '8/31/2017', 81.9)]

Я предполагаю, что в заголовках ваших столбцов могут быть пробелы.

0 голосов
/ 27 октября 2018

Как вы можете видеть из панд read_csv документы:

header : int or list of ints, default 'infer'
    Row number(s) to use as the column names, and the start of the
    data.  Default behavior is to infer the column names: if no names
    are passed the behavior is identical to ``header=0`` and column
    names are inferred from the first line of the file, if column
    names are passed explicitly then the behavior is identical to
    ``header=None``. Explicitly pass ``header=0`` to be able to
    replace existing names. The header can be a list of integers that
    specify row locations for a multi-index on the columns
    e.g. [0,1,3]. Intervening rows that are not specified will be
    skipped (e.g. 2 in this example is skipped). Note that this
    parameter ignores commented lines and empty lines if
    ``skip_blank_lines=True``, so header=0 denotes the first line of
    data rather than the first line of the file.

Это означает, что read_csv использует вашу первую строку в качестве имени заголовка.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...