United Kingdom
Breadcrumb navigation
Operations with Frovedis DataFrame
Technical ArticlesNov 1, 2021
Shoichiro Yokotani, Application Development Expert
AI Platform division
The following article introduces the operations with the Frovedis DataFrame.
By using Python, you can select the data format such as List, Dictionary, Tuple in the standard library as the data destination. In addition, the Python libraries pandas and Numpy provide flexible operation functions.
This article will focus specifically on pandas. pandas provides data analysts with functions for formating labeled data structures.
After merging, aggregating, and slicing multiple tables using pandas, you can check the statistical information of your data and perform analysis using data analysis algorithms. In order to handle data stored in List or Dictionary format, it is necessary to create processing code. With pandas, various processes can be realized using the functions of DataFrame. It is faster to use the pandas method than to make changes to the List or Dictionary format data by loop processing.
Also, the larger the data, the greater the difference in processing speed.
When handling relatively small data on SX-Aurora TSUBASA, you can execute analysis on the Vector Engine by getting the data with pandas DataFrame and converting it to Frovedis DataFrame. At this time, the data is transferred from the x86 CPU main memory to the Vector Engine memory.
The functionality provided by the Frovedis DataFrame is equivalent to a subset of the pandas version. You can use functions for exchanging data with pandas DataFrame as well as basic operation functions such as Select, Join, Sort, and Groupby. Data shaping can be made flexible by linking with mathematical arithmetic processing using Numpy's multidimensional array object, handling of time series data of pandas, and data input / output function.
Let's take a look at some examples of working with Frovedis DataFrame while using Jupyter notebook. First, perform data operations such as select, sort, and group by with small data.
Frovedis DataFrame operation example using sample data¶
import os
import numpy as np
import pandas as pd
from frovedis.exrpc.server import FrovedisServer
from frovedis.dataframe.df import FrovedisDataframe
Starting Frovedis server¶
FrovedisServer.initialize("mpirun -np 2 {}".format(os.environ['FROVEDIS_SERVER']))
'[ID: 1] FrovedisServer (Hostname: handson02, Port: 42383) has been initialized with 2 MPI processes.'
Two types of sample data are prepared in dictionary format. Converting to Frovedis DataFrame via pandas DataFrame. Data is transferred to the Vector Engine's memory as it is converted from pandas to Frovedis DataFrame.¶
peopleDF = {
'Ename' : ['Michael', 'Andy', 'Tanaka', 'Raul', 'Yuta'],
'Age' : [29, 30, 27, 19, 31],
'Country' : ['USA', 'England', 'Japan', 'France', 'Japan']
}
countryDF = {
'Ccode' : [1, 2, 3, 4],
'Country' : ['USA', 'England', 'Japan', 'France']
}
pdf1 = pd.DataFrame(peopleDF)
pdf2 = pd.DataFrame(countryDF)
fdf1 = FrovedisDataframe(pdf1)
fdf2 = FrovedisDataframe(pdf2)
Showing Frovedis Data Frame.¶
# display created frovedis dataframes
print ("* print Frovedis DataFrame")
print(fdf1.to_pandas_dataframe())
print(fdf2.to_pandas_dataframe())
* print Frovedis DataFrame Ename Age Country index 0 Michael 29 USA 1 Andy 30 England 2 Tanaka 27 Japan 3 Raul 19 France 4 Yuta 31 Japan Ccode Country index 0 1 USA 1 2 England 2 3 Japan 3 4 France
Column is selected by specifying the column name.¶
# select demo
print ("* select Ename and Age")
print(fdf1[["Ename","Age"]].to_pandas_dataframe())
* select Ename and Age Ename Age 0 Michael 29 1 Andy 30 2 Tanaka 27 3 Raul 19 4 Yuta 31
Filtering by specifying "age" and "country name".¶
# filter demo
print ("* filter by Age > 19 and Contry == 'Japan'")
print(fdf1[fdf1.Age > 19 and fdf1.Country == 'Japan'].to_pandas_dataframe())
* filter by Age > 19 and Contry == 'Japan' Ename Age Country 0 Tanaka 27 Japan 1 Yuta 31 Japan
Sorting by "age".¶
# sort demo
print ("* sort by Age (descending order)")
print(fdf1.sort("Age",ascending=False).to_pandas_dataframe()) # single column, descending
* sort by Age (descending order) Ename Age Country 0 Yuta 31 Japan 1 Andy 30 England 2 Michael 29 USA 3 Tanaka 27 Japan 4 Raul 19 France
Sorting by specifying multiple columns.¶
print ("* sort by Country and Age")
print(fdf1.sort(["Country", "Age"]).to_pandas_dataframe()) # multiple column
* sort by Country and Age Ename Age Country 0 Andy 30 England 1 Raul 19 France 2 Tanaka 27 Japan 3 Yuta 31 Japan 4 Michael 29 USA
Grouping by "country name", and totaling the maximum, minimum, and average ages in each group.¶
# groupby demo
print ("* groupby Country and max/min/mean of Age and count of Ename")
out = fdf1.groupby('Country')
out = out.agg({'Age': ['max','min','mean']})
out2 = out[["Country","max_Age","min_Age","mean_Age"]]
print(out2.to_pandas_dataframe())
* groupby Country and max/min/mean of Age and count of Ename Country max_Age min_Age mean_Age 0 England 30 30 30.0 1 Japan 31 27 29.0 2 France 19 19 19.0 3 USA 29 29 29.0
Renaming the column.¶
# renaming demo
print ("* rename Contry to Cname")
fdf3 = fdf2.rename({'Country' : 'Cname'})
print(fdf3.to_pandas_dataframe())
* rename Contry to Cname Ccode Cname index 0 1 USA 1 2 England 2 3 Japan 3 4 France
Two DataFrames are integrated by specifying the column name.¶
# join after column renaming
print ("* merge (join) two tables")
out = fdf1.merge(fdf3, left_on="Country", right_on="Cname") # with defaults
print(out.to_pandas_dataframe())
* merge (join) two tables Ename Age Country Ccode Cname index 0 Michael 29 USA 1 USA 1 Andy 30 England 2 England 2 Tanaka 27 Japan 3 Japan 3 Raul 19 France 4 France 4 Yuta 31 Japan 3 Japan
Integration and sorting of two DataFrames. Displaying the selected result.¶
# operation chaining: join -> sort -> select -> show
print ("* chaining: merge two tables, sort by Age, and select Age, Ename and Country")
out = fdf1.merge(fdf3, left_on="Country", right_on="Cname") \
.sort("Age")[["Age", "Ename", "Country"]]
print(out.to_pandas_dataframe())
* chaining: merge two tables, sort by Age, and select Age, Ename and Country Age Ename Country 0 19 Raul France 1 27 Tanaka Japan 2 29 Michael USA 3 30 Andy England 4 31 Yuta Japan
Displaying statistical information of data in DataFrame.¶
# column statistics
print ("describe: ")
print (fdf1.describe())
print ("\n")
describe: Age count 5.000000 mean 27.200000 std 4.816638 sum 136.000000 min 19.000000 max 31.000000
Creating a new Frovedis DataFrame "joined" by integrating pandas DataFrame and Frovedis DataFrame.¶
# merging with panda dataframe
print ("* merge with pandas table")
pdf2.rename(columns={'Country' : 'Cname'},inplace=True)
joined = fdf1.merge(pdf2, left_on="Country", right_on="Cname")
print(joined.to_pandas_dataframe())
* merge with pandas table Ename Age Country Ccode Cname index 0 Michael 29 USA 1 USA 1 Andy 30 England 2 England 2 Tanaka 27 Japan 3 Japan 3 Raul 19 France 4 France 4 Yuta 31 Japan 3 Japan
Converting Frovedis DataFrame to pandas DataFrame.¶
# conversion
print ("* convert Frovedis DataFrame to Pandas DataFrame")
print (fdf1.to_pandas_dataframe())
print ("\n")
* convert Frovedis DataFrame to Pandas DataFrame Ename Age Country index 0 Michael 29 USA 1 Andy 30 England 2 Tanaka 27 Japan 3 Raul 19 France 4 Yuta 31 Japan
FrovedisServer.shut_down()
Next, let's look at an operation example using Kaggle's Covid-19 vaccine data.