Ral*_*lph 6 python markdown pandas
我有一个 pandas DataFrame,其中有一列 dtype 日期时间:
import pandas as pd
# Mock-up data
df = pd.DataFrame({'year': [2015, 2016],
'month': [2, 3],
'day': [4, 5]})
df = pd.to_datetime(df)
print(df)
# 0 2015-02-04
# 1 2016-03-05
# dtype: datetime64[ns]
Run Code Online (Sandbox Code Playgroud)
我想使用该.to_markdown()方法来显示此 DataFrame。
但是,该.to_markdown()方法以科学计数法显示日期时间:
print(df.to_markdown())
# | | 0 |
# |---:|------------:|
# | 0 | 1.42301e+18 |
# | 1 | 1.45714e+18 |
Run Code Online (Sandbox Code Playgroud)
有没有办法让该.to_markdown()方法以更易于理解的方式显示这些日期?、和
方法已经.to_latex()以这种方式运行:.to_csv().to_string()
# Other .to_ methods behave as desired, eg.
print(df.to_latex())
# \begin{tabular}{ll}
# \toprule
# {} & 0 \\
# \midrule
# 0 & 2015-02-04 \\
# 1 & 2016-03-05 \\
# \bottomrule
# \end{tabular}
Run Code Online (Sandbox Code Playgroud)
熊猫版本:1.3.2
制表版本:0.8.9
.to_markdown()该方法在底层使用了该tabulate包。命名参数floatfmt可用于控制浮点数的格式,但我看不出这在这里有何用处。
目前我能找到的最佳解决方案是在调用该方法之前将日期时间列格式化为字符串列.to_markdown():
print(df.astype(str).to_markdown())
# | | 0 |
# |---:|:-----------|
# | 0 | 2015-02-04 |
# | 1 | 2016-03-05 |
Run Code Online (Sandbox Code Playgroud)