1 Answer
- Newest
- Most votes
- Most comments
1
The %table magic works only with the Spark DataFrame. I have confirmed that it does not generate the same output for Pandas DataFrame. As a workaround, you can convert the Pandas DataFrame to a Spark DataFrame before printing it out using the %table magic. Please refer to the sample code below:
import pandas as pd
# initialize list of lists
data = [[2, 'Alice'], [5, 'Bob']]
# Create the pandas DataFrame
df = pd.DataFrame(data, columns=['Age','Name'])
# Create a PySpark dataframe
df_spark = spark.createDataFrame(df)
# Display as a table
t = df_spark.collect()
%table t
However, I was able to format and print the Pandas DataFrame using %matplot. Listing the code below for your reference:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
# hide axes
fig.patch.set_visible(False)
ax.axis('off')
ax.axis('tight')
df = pd.DataFrame(np.random.randn(10, 4), columns=list('ABCD'))
ax.table(cellText=df.values, colLabels=df.columns, loc='center')
fig.tight_layout()
%matplot plt
answered 8 months ago
Relevant content
- asked 7 months ago
- asked 10 months ago
- AWS OFFICIALUpdated a year ago
- AWS OFFICIALUpdated a month ago
- AWS OFFICIALUpdated a month ago
- AWS OFFICIALUpdated 2 years ago