A
A
Alexey Smirnov2018-11-13 18:45:33
Python
Alexey Smirnov, 2018-11-13 18:45:33

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

As a result, I get:
5beaec2cebd11381062258.jpeg5beaec9a068ae510999338.jpeg
As you can see, all the data in the DataFrame of type object and in square brackets (perhaps this is the cause of the problem).
I need to build plots based on this data using the Matplotlib library. But if I try to plot with this data, I get an error: "setting an array element with a sequence.".
Then I try to change the data type of the DataFrame columns:
df['Горячая вода'] = df['Горячая вода'].astype(float)

And I get the same error: "setting an array element with a sequence.".
If I change the data type to the same (albeit a meaningless action, but I tried it):
df['Горячая вода'] = df['Горячая вода'].astype(object)

Then no error occurs.
How to solve this problem?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Smirnov, 2018-11-13
@ERAFY

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()

Next, let's create an empty DataFrame and write data from MS Access line by line into it.
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

That's it, the desired DataFrame is ready!

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question