# How to get dataframe as output in actions.py

**URL:** <https://forum.rasa.com/t/how-to-get-dataframe-as-output-in-actions-py/49244>\
**Category:** Rasa Open Source\
**Created:** [November 15, 2021, 9:48am UTC](https://forum.rasa.com/t/how-to-get-dataframe-as-output-in-actions-py/49244 "2021-11-15T09:48:52Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![gaurav21s](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/gaurav21s/32/18080_2.png) [@gaurav21s](https://forum.rasa.com/u/gaurav21s)\
**Post date:** [November 15, 2021, 9:48am UTC](https://forum.rasa.com/t/how-to-get-dataframe-as-output-in-actions-py/49244/1 "2021-11-15T09:48:52Z")

</div>

_I wanted to print dataframe of products in the actions.py with the help of a dispatcher.utter\_message or any other method in bot. I am getting an error is mentioned below:_

> Blockquote ERROR rasa.core.processor - Encountered an exception while running action ‘action\_get\_accessories’.Bot will continue, but the actions events are lost. Please check the logs of your action server for more information. Traceback (most recent call last): File “e:\anaconda\envs\chatbot\lib\site-packages\rasa\core\actions\action.py”, line 686, in run json=json\_body, method=“post”, timeout=DEFAULT\_REQUEST\_TIMEOUT File “e:\anaconda\envs\chatbot\lib\site-packages\rasa\utils\endpoints.py”, line 173, in request response.status, response.reason, await response.content.read() rasa.utils.endpoints.ClientResponseError: 500, Internal Server Error, body=‘b’\xe2\x9a\xa0\xef \xb8\x8f 500 \xe2\x80\x94 Internal Server Error\n\n html { font-family: sans-serif }\n h2 { color: #888; }\n .tb-w rapper p { margin: 0 }\n .frame-border { margin: 1rem }\n .frame-line \> \* { padding: 0.3rem 0.6rem }\n .frame-line { margin-botto m: 0.3rem }\n .frame-code { font-size: 16px; padding-left: 4ch }\n .tb-wrapper { border: 1px solid #eee }\n .tb-header { backgrou nd: #eee; padding: 0.3rem; font-weight: bold }\n .frame-descriptor { background: #e2eafb; font-size: 14px }\n \n
> 
> # \xe2\x9a\xa0\xef \xb8\x8f 500 \xe2\x80\x94 Internal Server Error
> 
> The server encountered an internal error and cannot complete your request.\n’’

The above exception was the direct cause of the following exception:

Traceback (most recent call last): File “e:\anaconda\envs\chatbot\lib\site-packages\rasa\core\processor.py”, line 773, in \_run\_action output\_channel, nlg, temporary\_tracker, self.domain File “e:\anaconda\envs\chatbot\lib\site-packages\rasa\core\actions\action.py”, line 709, in run raise RasaException(“Failed to execute custom action.”) from e rasa.shared.exceptions.RasaException: Failed to execute custom action.

**Also in the actions.py file I am getting this issue.**

> Blockquote Exception occurred while handling uri: ‘[http://localhost:5055/webhook](http://localhost:5055/webhook)’ Traceback (most recent call last): File “e:\anaconda\envs\chatbot\lib\site-packages\sanic\app.py”, line 931, in handle\_request response = await response File “e:\anaconda\envs\chatbot\lib\site-packages\rasa\_sdk\endpoint.py”, line 114, in webhook return response.json(result, status=200) File “e:\anaconda\envs\chatbot\lib\site-packages\sanic\response.py”, line 205, in json dumps(body, \*\*kwargs),

is not JSON serializable

---

<div class="post-metadata">

**Author:** ![ChrisRahme](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/chrisrahme/32/15572_2.png) [@ChrisRahme](https://forum.rasa.com/u/ChrisRahme)\
**Post date:** [November 15, 2021, 1:22pm UTC](https://forum.rasa.com/t/how-to-get-dataframe-as-output-in-actions-py/49244/2 "2021-11-15T13:22:22Z")

</div>

Welcome to the forum!

> [@gaurav21s](#):
>
> I wanted to print dataframe of products

I assume it’s a Pandas dataframe, which is not a string and you should not use it as one.

`dispatcher.utter_message()` takes in a string, therefore you need to convert the dataframe to a string.

As per the [Pandas documentation](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.to_string.html), use `dataframe.to_string()`.

---

<div class="post-metadata">

**Author:** ![nik202](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/nik202/32/16098_2.png) [@nik202](https://forum.rasa.com/u/nik202)\
**Post date:** [November 15, 2021, 2:06pm UTC](https://forum.rasa.com/t/how-to-get-dataframe-as-output-in-actions-py/49244/3 "2021-11-15T14:06:43Z")

</div>

@gaurav21s Hello, can you please share the `rasa --version`, are you fetching the data from `.csv` or `.xls` file? can you share the action.py code for our reference?

---

<div class="post-metadata">

**Author:** ![gaurav21s](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/gaurav21s/32/18080_2.png) [@gaurav21s](https://forum.rasa.com/u/gaurav21s)\
**Post date:** [November 15, 2021, 2:40pm UTC](https://forum.rasa.com/t/how-to-get-dataframe-as-output-in-actions-py/49244/4 "2021-11-15T14:40:49Z")

</div>

I have already tried that but the output is in not a readable format. I want it in a table-like format or in a better format.

---

<div class="post-metadata">

**Author:** ![gaurav21s](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/gaurav21s/32/18080_2.png) [@gaurav21s](https://forum.rasa.com/u/gaurav21s)\
**Post date:** [November 15, 2021, 2:46pm UTC](https://forum.rasa.com/t/how-to-get-dataframe-as-output-in-actions-py/49244/5 "2021-11-15T14:46:38Z")

</div>

Hey @nik202 my rasa version is 2.8.1 and I am fetching the data from .csv. I can’t share the whole code but I am sharing you the a part of it. ans = df[(df[‘gender\_m’].str.lower() == Gender.lower()) & ( df[‘product\_specific\_type’].str.contains(Product.lower(), case=False)) & (df[‘color\_m’].str.lower() == Colour.lower())]

```
    message = "Here are the products based on your choice, " + "\n" + "Name:" + json.dumps(
        ans['NAME'].values.tolist()) \
              + "\n" + "Product URL:" + json.dumps(ans['PRODUCT URL'].values.tolist()) \
              + "\n" + "Product Picture:" + json.dumps(ans['prod_pic'].values.tolist())
    # data = ans[['NAME', 'PRODUCT URL', 'prod_pic']]
    if ans["NAME"].empty:
        message = "The product is currently not available. You can enter your email id and we will notify you once " \
                  "the product is back in stock."

    print(ans.head())

    dispatcher.utter_message(message)
    return []

```

#This output is not very readable. Here is the data which I want as output.

 ![image](https://europe1.discourse-cdn.com/flex013/uploads/rasa/original/3X/4/b/4b3d01942ecd920c073c49a8a74989133bc3a61f.png)

---

<div class="post-metadata">

**Author:** ![nik202](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/nik202/32/16098_2.png) [@nik202](https://forum.rasa.com/u/nik202)\
**Post date:** [November 15, 2021, 5:09pm UTC](https://forum.rasa.com/t/how-to-get-dataframe-as-output-in-actions-py/49244/6 "2021-11-15T17:09:28Z")

</div>

@gaurav21s as expected, I just want to see the function of how are you fetch the .csv file using custom actions, can you share that and please do format the code for better understanding 🙂

---

<div class="post-metadata">

**Author:** ![ChrisRahme](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/chrisrahme/32/15572_2.png) [@ChrisRahme](https://forum.rasa.com/u/ChrisRahme)\
**Post date:** [November 15, 2021, 6:50pm UTC](https://forum.rasa.com/t/how-to-get-dataframe-as-output-in-actions-py/49244/7 "2021-11-15T18:50:50Z")

</div>

> [@gaurav21s](#):
>
> I want it in a table-like format or in a better format.

This is an issue with Pandas and/or general Python, not Rasa 🙂

As you can see in [their docs](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.to_string.html), the function has a lot of customization options.

You have a string and you want to properly format it, this is not Rasa-related and you would have better chances of finding someone who knows in places like [Stack Overflow](https://stackoverflow.com/) 🙂

But, you can still show us your function and we’ll try to help you with Python.

---

<div class="post-metadata">

**Author:** ![RodrigoLima](https://avatars.discourse-cdn.com/v4/letter/r/58956e/32.png) [@RodrigoLima](https://forum.rasa.com/u/RodrigoLima)\
**Post date:** [November 15, 2021, 9:04pm UTC](https://forum.rasa.com/t/how-to-get-dataframe-as-output-in-actions-py/49244/8 "2021-11-15T21:04:07Z")

</div>

![image](https://europe1.discourse-cdn.com/flex013/uploads/rasa/original/3X/f/8/f8f2d758060d412600599fe6f2ab45a6d4d82bea.png) ![image](https://europe1.discourse-cdn.com/flex013/uploads/rasa/original/3X/e/5/e58eec6e532444a10b7edaa344277a93fe3e5be5.png)

you can use iloc or loc inside the .py file and a for loop to output the utter\_message

---

<div class="post-metadata">

**Author:** ![gaurav21s](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/gaurav21s/32/18080_2.png) [@gaurav21s](https://forum.rasa.com/u/gaurav21s)\
**Post date:** [November 16, 2021, 3:59am UTC](https://forum.rasa.com/t/how-to-get-dataframe-as-output-in-actions-py/49244/9 "2021-11-16T03:59:55Z")

</div>

class ActionGetAccessories(Action): def name(self) → Text: return “action\_get\_accessories”

```
def run(
        self,
        dispatcher,
        tracker: Tracker,
        domain: "DomainDict",
) -> List[Dict[Text, Any]]:
    Product = tracker.get_slot("product")
    Gender = tracker.get_slot("gender")
    Colour = tracker.get_slot("colour")
    df = pd.read_csv("sample_product_db_v2.csv")
    ans = df[(df['gender_m'].str.lower() == Gender.lower()) & (
            df['product_specific_type'].str.contains(Product.lower(), case=False)) & (df['color_m'].str.lower() == Colour.lower())]

    message = "Here are the products based on your choice, " + "\n" + "Name:" + json.dumps(
        ans['NAME'].values.tolist()) \
              + "\n" + "Product URL:" + json.dumps(ans['PRODUCT URL'].values.tolist()) \
              + "\n" + "Product Picture:" + json.dumps(ans['prod_pic'].values.tolist())
    # data = ans[['NAME', 'PRODUCT URL', 'prod_pic']]
    if ans["NAME"].empty:
        message = "The product is currently not available. You can enter your email id and we will notify you once " \
                  "the product is back in stock."

    print(ans.head())

    dispatcher.utter_message(message)

```

Is there any other way to show data in the bot? Also, how can I show the previous orders in a button format so when the user asks for return products or change the address for the last order, he can select by button for which order he wants to see the details?

---

<div class="post-metadata">

**Author:** ![gaurav21s](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/gaurav21s/32/18080_2.png) [@gaurav21s](https://forum.rasa.com/u/gaurav21s)\
**Post date:** [November 16, 2021, 4:01am UTC](https://forum.rasa.com/t/how-to-get-dataframe-as-output-in-actions-py/49244/10 "2021-11-16T04:01:41Z")

</div>

return dados\_tabela will return the value in actions.py or in the bot?

---

<div class="post-metadata">

**Author:** ![RodrigoLima](https://avatars.discourse-cdn.com/v4/letter/r/58956e/32.png) [@RodrigoLima](https://forum.rasa.com/u/RodrigoLima)\
**Post date:** [November 22, 2021, 2:11am UTC](https://forum.rasa.com/t/how-to-get-dataframe-as-output-in-actions-py/49244/11 "2021-11-22T02:11:32Z")

</div>

> [@gaurav21s](#):
>
> eturn dados\_tabela will return the value in actions.py or in the bot?

It will return in the actions, there you fill the slots as needed or send the msg to the user with this data

---

<div class="post-metadata">

**Author:** ![RodrigoLima](https://avatars.discourse-cdn.com/v4/letter/r/58956e/32.png) [@RodrigoLima](https://forum.rasa.com/u/RodrigoLima)\
**Post date:** [November 22, 2021, 2:14am UTC](https://forum.rasa.com/t/how-to-get-dataframe-as-output-in-actions-py/49244/12 "2021-11-22T02:14:48Z")

</div>

> [@gaurav21s](#):
>
> Is there any other way to show data in the bot? Also, how can I show the previous orders in a button format so when the user asks for return products or change the address for the last order, he can select by button for which order he wants to see the details?

 ![image](https://europe1.discourse-cdn.com/flex013/uploads/rasa/original/3X/3/b/3bc0e14484caf4124fdc353dbcc27a2a5e88f5b7.png) ![image](https://europe1.discourse-cdn.com/flex013/uploads/rasa/original/3X/e/a/ea294b1e8d4505d87090e6d0c940fdf8bbf62d71.png)

---

<div class="post-metadata">

**Author:** ![AdarshTugao](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/adarshtugao/32/17645_2.png) [@AdarshTugao](https://forum.rasa.com/u/AdarshTugao)\
**Post date:** [February 7, 2022, 6:03pm UTC](https://forum.rasa.com/t/how-to-get-dataframe-as-output-in-actions-py/49244/13 "2022-02-07T18:03:48Z")

</div>

@gaurav21s hi RodrigoLima did you find the solution for this , I am also facing the same issue , not able to display table in bot ,but it is coming in command prompt

---

<div class="post-metadata">

**Author:** ![RodrigoLima](https://avatars.discourse-cdn.com/v4/letter/r/58956e/32.png) [@RodrigoLima](https://forum.rasa.com/u/RodrigoLima)\
**Post date:** [February 17, 2022, 2:03am UTC](https://forum.rasa.com/t/how-to-get-dataframe-as-output-in-actions-py/49244/14 "2022-02-17T02:03:35Z")

</div>

Sorry, i didnt see your question

Yes Do you need help?

---

<div class="post-metadata">

**Author:** ![gp1901](https://avatars.discourse-cdn.com/v4/letter/g/85f322/32.png) [@gp1901](https://forum.rasa.com/u/gp1901)\
**Post date:** [April 16, 2022, 11:21am UTC](https://forum.rasa.com/t/how-to-get-dataframe-as-output-in-actions-py/49244/15 "2022-04-16T11:21:14Z")

</div>

Hi I’m looking to diaplay a table format for the dataframe in rasa x,have you got experience with this? thanks

---

<div class="post-metadata">

**Author:** ![RodrigoLima](https://avatars.discourse-cdn.com/v4/letter/r/58956e/32.png) [@RodrigoLima](https://forum.rasa.com/u/RodrigoLima)\
**Post date:** [April 16, 2022, 2:03pm UTC](https://forum.rasa.com/t/how-to-get-dataframe-as-output-in-actions-py/49244/16 "2022-04-16T14:03:09Z")

</div>

Hi! I use two python libraries to do this. What exactly do you need?

---

<div class="post-metadata">

**Author:** ![gp1901](https://avatars.discourse-cdn.com/v4/letter/g/85f322/32.png) [@gp1901](https://forum.rasa.com/u/gp1901)\
**Post date:** [April 18, 2022, 4:31am UTC](https://forum.rasa.com/t/how-to-get-dataframe-as-output-in-actions-py/49244/17 "2022-04-18T04:31:55Z")

</div>

I used tabulate but the output text looks congested on the rasa x UI, it there an y better library which can be used to display the table in a neat format ?

---

<div class="post-metadata">

**Author:** ![RodrigoLima](https://avatars.discourse-cdn.com/v4/letter/r/58956e/32.png) [@RodrigoLima](https://forum.rasa.com/u/RodrigoLima)\
**Post date:** [April 23, 2022, 2:22am UTC](https://forum.rasa.com/t/how-to-get-dataframe-as-output-in-actions-py/49244/18 "2022-04-23T02:22:21Z")

</div>

I can use a dataframe\_image

> **[dataframe-image](https://pypi.org/project/dataframe-image/0.0.7/)**
>
> Embed pandas DataFrames as images in pdf and markdown files when converting from Jupyter Notebooks
