9

I'm trying to show a code block with sample output to clarify my question. I know how to add a code block (indenting with 4 space) but I can't figure out how to show the output.

import pandas as pd
df = pd.DataFrame({'item_id':[1,2,1,1,2], 
                'shop_id':['S1', 'S1', 'S1', 'S2', 'S2'], 
                'price':[10, 20, 10, 12, 18]})
df

In a Jupyter notebook, the above will print the dataframe and I have seen other posts displaying results. How can do it?

Attempts:

  1. I have gone through this post which suggests that I can copy and paste the output from Jupyter but that didn't work.
  2. I have tried to save my notebook as HTML and then insert the html snippet here. I thought when I run it it would display the output but it shows the entire html code instead.
  3. Take a screenshot of my notebook and insert a link to it here. But I don't think this is the proper way to ask a question.

Please see example of what I wanted to post:

Several sets of inputs and outputs where the input is a code block and the output is tabular data.

1 Answer 1

10

Open a terminal or command-line window, start the ipython interpreter, and run the same commands that you ran in your example. This will give you the following pretty formatted output which you can copy/paste as plain text.

To format a block of text as code, select the block of code to be formatted with the mouse and click the pair of curly brackets {} in the Stack Overflow markdown editor to format the selected text as a code block or surround with groups of three backticks. Each group of backticks should be positioned on a separate line as shown in this example.

```
Markdown and HTML are turned off in code blocks 
```

To highlight a block of text as Python code, precede the block of Python code with the following HTML comment followed by a blank space: <!-- language: python -->

$ ipython
Python 2.7.15rc1  
Type "copyright", "credits" or "license" for more information.

IPython 5.5.0 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.

In [1]: import pandas as pd
   ...: df1 = pd.DataFrame({'item_id':[1,2,1,1,2], 
   ...:                 'shop_id':['S1', 'S1', 'S1', 'S2', 'S2'], 
   ...:                 'price':[10, 20, 10, 12, 18]})
   ...: df1
   ...: 
Out[1]: 
   item_id  price shop_id
0        1     10      S1
1        2     20      S1
2        1     10      S1
3        1     12      S2
4        2     18      S2

In [2]: df2 = pd.DataFrame({'shop_id':['S1', 'S2'],
   ...:                     'shop_name':['shop1', 'shop2']})  
   ...: df2
   ...: 
Out[2]: 
  shop_id shop_name
0      S1     shop1
1      S2     shop2

In [3]: shop_map = {shop_id:shop_name for shop_id,shop_name in zip(df2.shop_id, 
   ...: df2.shop_name)}
   ...: shop_map
   ...: 
Out[3]: {'S1': 'shop1', 'S2': 'shop2'}

In [4]: df1['shop_name'] = df1['shop_id'].apply(lambda shop_id: shop_map[shop_id
   ...: ])
   ...: df1
   ...: 
Out[4]: 
   item_id  price shop_id shop_name
0        1     10      S1     shop1
1        2     20      S1     shop1
2        1     10      S1     shop1
3        1     12      S2     shop2
4        2     18      S2     shop2

In [5]:
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .