Answer the question
In order to leave comments, you need to log in
How to solve "setting an array element with a sequence" error when converting data types of DataFrame columns?
Hello.
I am extracting data from a MS Access database into a Pandas DataFrame, for further plotting with the Matplotlib library. Retrieving data in a DataFrame is done with the following code:
import pyodbc
conn_str = (
r'DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};'
r'DBQ=C:\MEGA\Programming\Data_Bases\Microsoft Access\Показания.accdb;'
)
cnxn = pyodbc.connect(conn_str)
cursor = cnxn.cursor()
import pandas as pd
df = pd.DataFrame({
'Дата': cursor.execute('select "Дата" from TABLE').fetchall(),
'Электроэнергия': cursor.execute('select "Электроэнергия" from TABLE').fetchall(),
'Горячая вода': cursor.execute('select "Горячая вода" from TABLE').fetchall(),
'Холодная вода': cursor.execute('select "Холодная вода" from TABLE').fetchall(),
})
df
df['Горячая вода'] = df['Горячая вода'].astype(float)
df['Горячая вода'] = df['Горячая вода'].astype(object)
Answer the question
In order to leave comments, you need to log in
Decision.
First, let's connect to the MS Access database.
import pyodbc
conn_str = (
r'DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};'
r'DBQ=C:\MEGA\Programming\Data_Bases\Microsoft Access\Показания.accdb;'
)
cnxn = pyodbc.connect(conn_str)
cursor = cnxn.cursor()
import pandas as pd
df = pd.DataFrame(columns=['Дата', 'Электроэнергия', 'Горячая вода', 'Холодная вода'])
cursor.execute('select * from TABLE')
i=0
for row in cursor.fetchall():
df.loc[i] = [row[0], row[1], row[2], row[3]]
i=i+1
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question