Python

【Python】From DataFrame To list type

投稿日:

Overview

Pandas can get data from a database with read_sql easily.
Here we can show how to convert dataframe to list type for only one row from database.

Example table in databse

a,b,c are columns of this test_table.

abc
110100
220200
330300
440400
test_table

Let’s get only “a” column from this table with pandas.
These are sample code.

import pandas as pd
import psycopg2

connection = psycopg2.connect(host='host', dbname='database', user='username', password='password')
df = pd.read_sql("SELECT a FROM test_table", connection)
df.head()

Result

If you want to convert this result from dataframe to list type, you may think like [1, 2, 3, 4].

There is a simple method to convert dataframe to list, values.tolist().

This is a example to convert with values.tolist().

df.values.tolist()

#Result
[[1], [2], [3], [4]]

You can find the result is a little different from the list type we want.
That is double list but we want single one.

But it is quite normal because the result from sql is dataframe even thought it has just one column.

Solution

To use itertools library, you can get single list type from the dataframe.

This is the example

import itertools
a = df.values.tolist()
list(itertools.chain.from_iterable(a))

#result
[1, 2, 3, 4]

This method is so useful. Please try it.

-Python

執筆者:


comment

Your email address will not be published. Required fields are marked *

関連記事

Connection to PostgreSQL, Oracle&MySQL from Python

Contents1 Overview2 How to connect PostgreSQL3 Connect to MySQL4 Oracleの場合 Overview There are some samples to connect PostgreSQL, Oracle, MySQL from Python. How to connect PostgreSQL Package installation pip install psycopg2 Example import psycopg2 HOST = ‘your_host’ PORT = ‘5432’ DB_NAME = ‘your_db_name’ USER = ‘your_user_name’ PASSWORD = ‘your_password’ conn = psycopg2.connect("host=" + HOST + " port=" + PORT + " dbname=" + DB_NAME + " user=" + USER + " password=" + PASSWORD ) cur = conn.cursor() cur.execute("select version()") rows = cur.fetchall() cur.close() conn.close() print(rows) Connect to MySQL Package installation pip install mysqlclient Example import MySQLdb HOST = ‘your_host’ …

Anaconda installation on CentOS

Contents1 Overview2 Environments3 What is Anaconda4 Step1 : pyenvのインストール5 Step2 : Installation of Anaconda3 Overview Anaconda package is very useful tool for data analysis by python language. This article shows how to install it on CentOS. Environments OS:CentOS7Anaconda3:3-5.3.1 What is Anaconda Nowadays the usage of python language is being more popular than before for the purpose of machine learning or processing data. Anaconda package is consists of not only python engine but also IDE, web-base editor or some useful tools. When you just install anaconda, you can use these bundled useful tools. CentOS7 has python 2 as a default but …

【Python】Not getting all rows with BeautifulSoup

Contents1 Overview2 Environment3 Problem occurred example3.0.1 Result4 The way to fix it Overview When Scraping with Beautiful Soup a problem occurred like not getting all rows of the table but a few of them. This example shows how to fix it Environment Python 3.7.3 Problem occurred example This is a example table you want to scrape. NumberName1Sato2Kato3Ito4Goto I saw the html code in a web browser by pushing F12 key. <table class="test_table"> <thead> <tr> <th>Number</th> <th>Name</th> </tr> </thead> <tbody> <tr> <td>1</td> <td>Sato</td> </tr> </tbody> <tbody> <tr> <td>2</td> <td>Kato</td> </tr> </tbody> <tbody> <tr> <td>3</td> <td>Ito</td> </tr> </tbody> <tbody> <tr> <td>4</td> <td>Goto</td> …

Python : Insert dataframe data into MySQL table 

Contents1 Overview2 Environments3 Step1 : Making the table4 Step2 : Making data5 Step3 : Inserting data into MySQL table6 Additional explanation Overview Dataframe type in python is so useful to data processing and it’s possible to insert data as dataframe into MySQL . There is a sample of that. Environments Python 3.7.3MySQL 5.5.62 Step1 : Making the table Defining a table like the following. > CREATE DATABASE testdb; > CREATE TABLE testdb.mysql_table( col1 int ,col2 int ,col3 int ); Step2 : Making data Making data for insert from python. the data should be the same type as the table you …

【Python】Changing images periodically with tkinter

Contents1 Overview2 Python library to be used3 Step1 : Display a image with tkintertkinter4 Step2 : Changing images5 Summary Overview There is a good library in python to make GUI, that is tkinter. It can display images too. Here, we are trying to change images periodically with tkinter. Python library to be used Following three libraries are used. tkinter ‘tkinter’ is used to make GUI. PIL(pillow) ‘PIL’ is used to deal with images in the python. threading ‘threading’ is used to change images automatically. Detail about that will be explained later. By the way, if you are using anaconda which …

Language Switcher

Categories