DataFrameのデータを整形したい
Pandasを使っているとDataFrameのデータをそのままmarkdownに貼りたいとか、slackに貼りたいって時がって、その時に使います。
import pandas as pd
from tabulate import tabulate
df=pd.DataFrame.from_dict({
'A': [1,2,3],
'B': [4,5,6],
'C': [7,8,9],
})
print(tabulate(df, df.columns,tablefmt='github', showindex=False))
# 出力結果
| A | B | C |
|-----|-----|-----|
| 1 | 4 | 7 |
| 2 | 5 | 8 |
| 3 | 6 | 9 |
これでデータをメール、チャットに貼り付けたりしてる。