What are some ways to post python pandas dataframes to slack?

不羁的心 提交于 2019-12-04 04:14:13

There is a .to_html() method on DataFrames, so that might work. But if you are just looking to cut and paste, Tabulate is a good choice. From the docs:

from tabulate import tabulate
df = pd.DataFrame([["Name","Age"],["Alice",24],["Bob",19]])
print tabulate(df, tablefmt="grid")

Returns

+---+-------+-----+
| 0 | Name  | Age |
+---+-------+-----+
| 1 | Alice | 24  |
+---+-------+-----+
| 2 | Bob   | 19  |
+---+-------+-----+

Paste that in a code block in Slack and it should show up nicely.

Slack doesn't seem to take arbitrary HTML input or format text with escaped newlines which limits to plain text. tabulate as suggested in another answer works, but if you want something that is self contained this would work. Assuming your data frame is in df

  1. In python terminal session, jupyter notebook, or print statement run something like print(repr(df)).
  2. Copy the output
  3. Paste in code blocks in slack.

For the data frame I had on hand this is what you would enter in the slack chat box before hitting enter.

```
   code    sid state        triplet
0  SCAN   2057    AL   2057:AL:SCAN
1  SNOW    ABY    CA    ABY:CA:SNOW
2  SNOW  15A21    MT  15A21:MT:SNOW
3  COOP   0010    ID   0010:ID:COOP
4  SNOW  1F01A    BC  1F01A:BC:SNOW
```

If you wanted to integrate this as a web service replace the copy/paste stuff with web services/hooks.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!