Given a single table, how can we split it into multiple tables?


#1

Question

In this exercise, it mentions splitting the table into multiple tables. Given a single table, how can we split it into multiple tables?

Answer

In this context, the tables are dataframes. Because they are dataframes, we just need to split the dataframe into separate CSV files using methods from Pandas.

The following example will split a dataframe (“table”) into separate dataframes (“tables”), and save them into separate CSV files, to make them more manageable to work with.

Example

# First, read the original CSV and store in 
# a variable. This is the original "table".
data = pd.read_csv("filename.csv")

# Split the dataframe into separate ones, by columns.
# These are the "tables".
df1 = data[['col1', 'col2']]
df2 = data[['col3', 'col4', 'col5']]
df3 = data[['col6', 'col7', 'col8']]

# Now, we just need to store these into 
# their own CSV files.
# Note: index=False is used to remove the added index column.
df1.to_csv('table1.csv', index=False)
df2.to_csv('table2.csv', index=False)
df3.to_csv('table3.csv', index=False)