Display two dataframes side by side in Pandas

一笑奈何 提交于 2021-02-04 07:19:24

问题


I have two dataframes each with 10 rows and I am trying to display them side by side using

print df, df2

But it is giving output like this

               Last       High   High_Perc
170     0.01324000 0.03822200 65.36026372
194     0.00029897 0.00052040 42.54996157
163     0.00033695 0.00058000 41.90517241
130     0.00176639 0.00282100 37.38426090
78      0.00003501 0.00005552 36.94164265
13      0.00009590 0.00014814 35.26393952
58      0.00002149 0.00003228 33.42627014
124     0.00009151 0.00013700 33.20437956
32      0.00059649 0.00089000 32.97865169         Last        Low    Low_Perc
170     0.01324000 0.01204685 65.36026372
194     0.00029897 0.00029000 42.54996157
163     0.00033695 0.00032270 41.90517241
130     0.00176639 0.00171874 37.38426090
78      0.00003501 0.00003450 36.94164265
13      0.00009590 0.00009200 35.26393952
58      0.00002149 0.00002140 33.42627014
124     0.00009151 0.00009000 33.20437956
32      0.00059649 0.00059001 32.97865169

I have tried the below options but I am still not able to set them side by side

    pd.set_option('display.max_rows', 500)
    pd.set_option('display.max_columns', 500)
    pd.set_option('display.width', 10000)
    pd.set_option('display.float_format', lambda x: '%.8f' % x)
    pd.options.display.max_columns = None

Please help


回答1:


Unfortunately, print does not know to tile the dataframes side-by-side when printing them out. It calls each df's str and prints them one by one.

Try concatenation before the print:

print pd.concat([df.reset_index(drop=1).add_suffix('_1'),
            df2.reset_index(drop=1).add_suffix('_2')], axis=1).fillna('')



回答2:


Try something like this:

import izip_longest from itertools
print '\n'.join([ia + ib from ia, ib in izip_longest(
    str(df).split('\n'), str(df2).split('\n'), fillvalue = '')])

If the last is longer, you can adjust the length of the fill value.



来源:https://stackoverflow.com/questions/46859802/display-two-dataframes-side-by-side-in-pandas

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!