With jupyter notebook, this code
JavaScript
x
4
1
import pandas as pd
2
df = pd.DataFrame([[71,62,13], [75,76,77]], columns=list("ABC"))
3
df
4
gives output in this style
if I put it in a function,
JavaScript
1
6
1
def prepare():
2
import pandas as pd
3
df = pd.DataFrame([[71,62,13], [75,76,77]], columns=list("ABC"))
4
print(df)
5
prepare()
6
I get the dataframe in this style
How do I render the dataframe in the style at the beginning in a function?
Advertisement
Answer
The styling as you show, can be explicitly called by the head() method
Following on the comments, instead of having the function print the dataframe, you can have it return, the use the head method
Also, its a better practice to import the libraries outside of functions, but in the main file
JavaScript
1
9
1
import pandas as pd
2
3
def prepare():
4
df = pd.DataFrame([[71,62,13], [75,76,77]], columns=list("ABC"))
5
rerurn df
6
7
returned_df = prepare()
8
returned_df.head()
9