0

How would I insert a URL link at the top cell of my pandas dataframe?

myurl='www.xyz.com'

Let's say this is my code:

import pandas as pd
import numpy as np

dates = pd.date_range('20130101', periods=4)
df = pd.DataFrame(np.random.randn(6,4), columns=dates)

Output of the code:

   2013-01-01  2013-01-02  2013-01-03  2013-01-04
0   -1.359604    2.236055   -1.247478   -0.466911
1    0.632732    1.177155    0.572847    0.024058
2    0.301902   -0.118306   -1.162931    0.180230
3   -0.851283   -0.427693    0.070223   -1.469248
4   -0.309400    0.935575    1.938843    1.898458
5   -0.598406    0.519100   -0.700112    0.539412

Desired output1:

www.xyz.com
   2013-01-01  2013-01-02  2013-01-03  2013-01-04
0   -1.359604    2.236055   -1.247478   -0.466911
1    0.632732    1.177155    0.572847    0.024058
2    0.301902   -0.118306   -1.162931    0.180230
3   -0.851283   -0.427693    0.070223   -1.469248
4   -0.309400    0.935575    1.938843    1.898458
5   -0.598406    0.519100   -0.700112    0.539412

Desired output2:

   2013-01-01  2013-01-02  2013-01-03  2013-01-04
0   www.xyz.com
1   -1.359604    2.236055   -1.247478   -0.466911
2    0.632732    1.177155    0.572847    0.024058
3    0.301902   -0.118306   -1.162931    0.180230
4   -0.851283   -0.427693    0.070223   -1.469248
5   -0.309400    0.935575    1.938843    1.898458
6   -0.598406    0.519100   -0.700112    0.539412

1 Answer 1

1

Is this what you want ? (rename the column and index name)

df.rename_axis('www.xyz.com',axis=1)

www.xyz.com  2013-01-01  2013-01-02  2013-01-03  2013-01-04
0             -0.168198   -1.129815    0.427983   -0.788428
1              0.382714    0.539336   -0.568658    0.783393
2             -0.738407    0.276275    0.347246    0.956509
3              1.620627    0.373145   -0.308667   -1.366621
4              0.778496   -0.432625   -0.863804    1.362475
5              0.629325    0.435807   -0.681608   -0.077567

df.rename_axis('www.xyz.com',axis=0)
Out[138]: 
             2013-01-01  2013-01-02  2013-01-03  2013-01-04
www.xyz.com                                                
0             -0.168198   -1.129815    0.427983   -0.788428
1              0.382714    0.539336   -0.568658    0.783393
2             -0.738407    0.276275    0.347246    0.956509
3              1.620627    0.373145   -0.308667   -1.366621
4              0.778496   -0.432625   -0.863804    1.362475
5              0.629325    0.435807   -0.681608   -0.077567

Base on your requirement ..

Desired output2:

pd.concat([pd.DataFrame({df.columns[0]:['www.xyz.com']}),df],axis=0).fillna('').reset_index(drop=True)
Out[146]: 
    2013-01-01 2013-01-02 2013-01-03 2013-01-04
0  www.xyz.com                                 
1    -0.168198   -1.12982   0.427983  -0.788428
2     0.382714   0.539336  -0.568658   0.783393
3    -0.738407   0.276275   0.347246   0.956509
4      1.62063   0.373145  -0.308667   -1.36662
5     0.778496  -0.432625  -0.863804    1.36248
6     0.629325   0.435807  -0.681608 -0.0775674

and

Desired output1:

print('www.xyz.com','\n',df)
www.xyz.com 
    2013-01-01  2013-01-02  2013-01-03  2013-01-04
0   -0.168198   -1.129815    0.427983   -0.788428
1    0.382714    0.539336   -0.568658    0.783393
2   -0.738407    0.276275    0.347246    0.956509
3    1.620627    0.373145   -0.308667   -1.366621
4    0.778496   -0.432625   -0.863804    1.362475
5    0.629325    0.435807   -0.681608   -0.077567
Sign up to request clarification or add additional context in comments.

1 Comment

No, I want it at the top...desired output1. Desired output 2 is just so i can learn how to insert in the first row and renumber the index. Could you help?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.