我发现 pandas read_csv 方法比 numpy loadtxt 更快。不幸的是,现在我发现自己处于必须返回 numpy 的情况,因为 loadtxt 可以选择设置comments=['#','@']. Pandas read_csv 方法只能采用一个注释字符串,就像comment='#'我从帮助站点上看到的那样。有什么建议或解决方法可以让我的生活更轻松,并使我不再回到 numpy?还有为什么pandas不支持多评论指标?
# save this in test.dat
@ bla
# bla
1 2 3 4
Run Code Online (Sandbox Code Playgroud)
最小的例子:
# does work, but only one type of comment is accounted for
df = pd.read_csv('test.dat', index_col=0, header=None, comment='#')
# does not work (not suprising reading the help)
df = pd.read_csv('test.dat', index_col=0, header=None, comment=['#','@'])
# does work but is slow
df = np.loadtxt('test.dat', comments=['#','@'])
Run Code Online (Sandbox Code Playgroud) pandas to_latex()方法似乎是将更大的tabulars转换为乳胶代码的便捷方式.但是,写数学模式,$似乎逃脱了.
一个例子:
import pandas as pd
import numpy as np
table=np.asarray([['$x^2$',3]])
df=pd.DataFrame(table[:,1], columns=['value'], index=table[:,0])
print(df.to_latex(encoding='utf-8'))
Run Code Online (Sandbox Code Playgroud)
输出:
\begin{tabular}{ll}
\toprule
{} & value \\
\midrule
\$x\textasciicircum2\$ & 3 \\
\bottomrule
\end{tabular}
Run Code Online (Sandbox Code Playgroud)
有没有办法防止这种情况发生?
我喜欢包含变量的字符串的新格式选项,但我希望有一个变量来设置我的脚本的精度,我不知道如何做到这一点.让我举一个小例子:
a = 1.23456789
out_str = 'a = {0:.3f}'.format(a)
print(out_str)
Run Code Online (Sandbox Code Playgroud)
现在这是我想要在伪代码中做的事情:
a = 1.23456789
some_precision = 5
out_str = 'a = {0:.(some_precision)f}'.format(a)
print(out_str)
Run Code Online (Sandbox Code Playgroud)
但我不确定,如果可能的话,可能是语法的样子.