pandas 如何在python中打印出数据框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/49826909/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to print out dataframe in python
提问by chowpay
normally using jupyternotes I can import pandas make my dataframe and export to csv.
通常使用 jupyternotes 我可以导入Pandas制作我的数据框并导出到 csv。
I'm trying to automate this reoccurring query with a python script. I can't figure out how to test my script because I cant print out the df to screen. In jupyternotes I just need to type out the df name in any cell and it will display.
我正在尝试使用 python 脚本自动执行这个重复出现的查询。我不知道如何测试我的脚本,因为我无法将 df 打印到屏幕上。在 jupyternotes 中,我只需要在任何单元格中输入 df 名称,它就会显示出来。
here what I'm doing
这是我在做什么
import pandas as pd
def run_agg_query(db):
df1 = pd.read_sql( "select some query",conn)
display(df1)
but I cant get the df to display.
但我无法显示 df。
采纳答案by chowpay
turns out print(df)
works just fine it just took a long time to render due to the size of the table. I tried the suggestions above but had strange errors, eventually this worked print(df['colName'])
which lead me to the above... sometimes the solution is the obvious one *facepalm.
结果证明print(df)
效果很好,由于表格的大小,渲染时间很长。我尝试了上面的建议,但出现了奇怪的错误,最终这print(df['colName'])
使我得到了上述建议……有时解决方案是显而易见的 *facepalm。
回答by rowana
I think printing the dataframe as a string should do the job for you. Something like this might work -
我认为将数据帧打印为字符串应该可以为您完成这项工作。像这样的事情可能会奏效-
print(df.to_string())
Hope this helps!
希望这可以帮助!
回答by sarveswara rao vangala
Use this:
用这个:
print("data frame is:\n{}".format(data_pandas))