Dataframe Pandas PostgreSQl#
!pip install psycopg2
Requirement already satisfied: psycopg2 in c:\users\lenovo\appdata\local\programs\python\python310\lib\site-packages (2.9.5)
!pip install psycopg2-binary
Requirement already satisfied: psycopg2-binary in c:\users\lenovo\appdata\local\programs\python\python310\lib\site-packages (2.9.5)
import psycopg2
import pandas as pd
koneksi = psycopg2.connect(user="postgres", password="20072003", host="127.0.0.1", port="5432", database="postgres")
curr=koneksi.cursor()
curr.execute("SELECT * FROM public.iris_dataset;")
hasil=curr.fetchall()
column_names = ["sepal_length", "sepal_width", "petal_length", "petal_width","variety"]
df = pd.DataFrame(hasil, columns=column_names)
df.head()
| sepal_length | sepal_width | petal_length | petal_width | variety | |
|---|---|---|---|---|---|
| 0 | 5.1 | 3.5 | 1.4 | 0.2 | Setosa |
| 1 | 4.9 | 3 | 1.4 | 0.2 | Setosa |
| 2 | 4.7 | 3.2 | 1.3 | 0.2 | Setosa |
| 3 | 4.6 | 3.1 | 1.5 | 0.2 | Setosa |
| 4 | 5 | 3.6 | 1.4 | 0.2 | Setosa |