Home > Mobile >  Display data side by side
Display data side by side

Time:08-17

By using pandas.dataframe, extracting data using df.iloc, and applying some operations, I defined X, and Y as:

df.iloc[:,1] = XX[df.iloc[:,1],df.iloc[:,2],def.iloc[:,3]]             

and

df.iloc[:,2] = ZZ[df.iloc[:,1],df.iloc[:,2],def.iloc[:,3]]

I defined

X = df.iloc[:,1] 

and

Y = df.iloc[:,2]

NB: XX and ZZ are just matrices coordinates that I defined using numpy.meshgrid (no problem with that) Now, when I do print(X) and print(Y), I obtain the correct output. When I do print(X,Y), I got X and Y one under the other, like this :

0     -1.7833
1      3.6500
2     -3.5833
3      4.5167
4      1.1167
Name: X, Length: 4, dtype: float64
0      0.0167
1      0.0500
2      0.1500
3      0.0500
4      0.1500
Name: Y, Length: 4, dtype: float64

I want to display X and Y side by side, to obtain something like:

     label          X              Y  
0        1      -1.7833          0.0167         
1        2       3.6500          0.0500        
2        3      -3.5833          0.1500        
3        4       4.5167          0.0500  

   

I search on internet and I found that I need to use display_html. I got a problem to install display_html(I used pip install python and from python.display import display_html) but it doesn't work, the pip still giving the message: You may need to restart the kernel to use updates packages even if I did in the jupyter Kernel>restart and run all !

Does someone know how I can display this data side by side. It doesn't matter for me to use display_html or something else! Thank you

CodePudding user response:

If both series X, Y have equal index (which is your case, I guess), then we can use pandas.concat along the second axis:

print(pd.concat([X,Y], axis=1))

As an option, transform one to DataFrame and join with other:

print(x.to_frame().join(y))

In general indexes may differ. So then we could try to reset them and then apply either concat or join:

# fancy data
x = pd.Series(range(10,30), name='X', index=range(40,20,-1))
y = pd.Series([*'abcdefghkl'], name='Y', index=range(100,110))

# print with pd.concat method
def my_concat(*args):
    out = pd.concat(
        [
            x
            .rename_axis(index=f'index_{x.name}')
            .reset_index()
            for x in args
        ],
        axis=1
    )
    return out.fillna('')

print(my_concat(x, y).to_string(index=False))

# print with .join mathod
print(
    x.reset_index()
    .join(
        y.reset_index(), 
        how='outer', 
        lsuffix=f'_{x.name}',
        rsuffix=f'_{y.name}'
    ).fillna('')
    .to_string(index=False)
)

CodePudding user response:

Here the DataFrame will be join based on the index -

pd.merge(x, y, left_index=True, right_index=True)

or

we can use a common column for joining (It is similar to SQL join)

pd.merge(x, y, on = 'give_common_col_name')

  • Related