Python Hana - ML - Define HANA Table and Upload (Create - Dataframe - From - Pandas) - SAP Blogs
Python Hana - ML - Define HANA Table and Upload (Create - Dataframe - From - Pandas) - SAP Blogs
Technical Articles
Yohei Fukuhara
September 6, 2022
| 3 minute read
RSS Feed I am writing this blog to define HANA tables and upload data to the table using
python package hana_ml.
Environment
Environment is as below.
hana_ml: 2.13.22072200
pandas: 1.3.5
scikit-learn: 1.0.2
As for HANA Cloud, I activated scriptserver and created my users. Though I don’t
recognize other special configurations, I may miss something since our HANA
Cloud was created long time before.
https://blogs.sap.com/2022/09/06/python-hana_ml-define-hana-table-and-uploadcreate_dataframe_from_pandas/ 1/7
9/6/22, 3:07 PM Python hana_ml: define HANA table and upload(create_dataframe_from_pandas) | SAP Blogs
Python Script
1. Install Python packages
Install python package hana_ml, which is not pre-installed on Google
Colaboratory.
Collecting hdbcli==2.13.13
Collecting htmlmin>=0.1.12
https://blogs.sap.com/2022/09/06/python-hana_ml-define-hana-table-and-uploadcreate_dataframe_from_pandas/ 2/7
9/6/22, 3:07 PM Python hana_ml: define HANA table and upload(create_dataframe_from_pandas) | SAP Blogs
2. Import modules
Import python package modules.
PASS = '<PASSWORD>'
password=PASS, schema=SCHEMA)
print(conn.hana_version())
4.00.000.00.1660640318 (fa/CE2022.16)
def make_df():
X, y = make_classification(n_samples=1000,
n_features=3, n_redundant=0)
df['CLASS'] = y
return df
df = make_df()
print(df)
df.info()
X1 X2 X3 CLASS
https://blogs.sap.com/2022/09/06/python-hana_ml-define-hana-table-and-uploadcreate_dataframe_from_pandas/ 3/7
9/6/22, 3:07 PM Python hana_ml: define HANA table and upload(create_dataframe_from_pandas) | SAP Blogs
<class 'pandas.core.frame.DataFrame'>
The function is very useful, since it automatically define table and upload at the
same time. Please check options for further detail.
TRAIN_TABLE = 'PAL_TRAIN'
schema=SCHEMA,
def show_hana_df(dfh):
print(dfh.collect())
https://blogs.sap.com/2022/09/06/python-hana_ml-define-hana-table-and-uploadcreate_dataframe_from_pandas/ 4/7
9/6/22, 3:07 PM Python hana_ml: define HANA table and upload(create_dataframe_from_pandas) | SAP Blogs
print(dfh.describe().collect())
show_hana_df(dfh)
X1 X2 X3 CLASS
7. Close connection
Last but not least, closing connection explicitly is preferable.
conn.close()
Alert Moderator
https://blogs.sap.com/2022/09/06/python-hana_ml-define-hana-table-and-uploadcreate_dataframe_from_pandas/ 5/7
9/6/22, 3:07 PM Python hana_ml: define HANA table and upload(create_dataframe_from_pandas) | SAP Blogs
Alert Moderator
Assigned Tags
SAP HANA
Python
hana_ml
hana-ml
Related Questions
Error while calling procedure from python sqlalchemy
By
Arunkumar KP Mar 11, 2020
batch insert of tuples into HANA using HANA Client in SAP Data Intelligence
By
Rajarshi Muhuri Jul 16, 2021
Datatype guessing
By
Marvin Kennel Sep 01, 2022
https://blogs.sap.com/2022/09/06/python-hana_ml-define-hana-table-and-uploadcreate_dataframe_from_pandas/ 6/7
9/6/22, 3:07 PM Python hana_ml: define HANA table and upload(create_dataframe_from_pandas) | SAP Blogs
Find us on
Newsletter Support
https://blogs.sap.com/2022/09/06/python-hana_ml-define-hana-table-and-uploadcreate_dataframe_from_pandas/ 7/7