我正在使用pandas.read_sql()命令从我的 postgresql 数据库中获取数据。SQL 查询通常使用许多列创建,我只想使用一列作为索引从中获取特定列。创建这样的示例表test_table:
column1 column2 column3
1 2 3
2 4 6
3 6 9
Run Code Online (Sandbox Code Playgroud)
我尝试使用index_colandcolumns参数 frompandas.read_sql()来获取column1索引和column2数据(并忽略column3!)。但它总是返回整个表。也当写columns=['column1', 'column2']什么都没有改变......
我正在使用 python 2.7.6 和 pandas 0.17.1 - 感谢您的帮助!
示例代码:
import pandas
import psycopg2
import sqlalchemy
def connect():
connString = (
"dbname=test_db "
"host=localhost "
"port=5432 "
"user=postgres "
"password=password"
)
return psycopg2.connect(connString)
engine = sqlalchemy.create_engine(
'postgresql://',
creator=connect)
sql = (
'SELECT '
'column1, …Run Code Online (Sandbox Code Playgroud) 我想pandas.Dataframe用 locale渲染一个"de_DE.UTF-8",有一个“,”作为小数点和“。” 作为千位分隔符。简单地运行时locale.format,我得到了预期的结果。但是当添加与 Pandas 格式化程序相同的表达式时,渲染的 html 没有任何变化(尽管没有抛出错误)。示例代码:
import pandas
import locale
locale.setlocale(locale.LC_NUMERIC, 'de_DE.UTF-8')
print(locale.format('%.2f', 1000.245, True))
print(locale.format('%.2f', 10000000.22655, True))
df = pandas.DataFrame({'a': [1000.245, 10000000.22655]})
style = df.style.format(formatter=lambda x: f'{locale.format("%.2f", x, True)} €')
print(style.render())
Run Code Online (Sandbox Code Playgroud)
给出输出:
1.000,25
10.000.000,23
<style type="text/css" >
</style>
<table id="T_f0ae1678_3e71_11e9_8f47_d0bf9ce00d56" >
<thead> <tr>
<th class="blank level0" ></th>
<th class="col_heading level0 col0" >a</th>
</tr></thead>
<tbody> <tr>
<th id="T_f0ae1678_3e71_11e9_8f47_d0bf9ce00d56level0_row0" class="row_heading level0 row0" >0</th>
<td id="T_f0ae1678_3e71_11e9_8f47_d0bf9ce00d56row0_col0" class="data row0 col0" >1000.25 €</td>
</tr> <tr>
<th id="T_f0ae1678_3e71_11e9_8f47_d0bf9ce00d56level0_row1" …Run Code Online (Sandbox Code Playgroud)