如何在pandas dataframe的顶部单元格上插入一个URL链接?
myurl ='www.xyz.com'
假设这是我的代码:
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)
代码的输出:
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
所需的输出1:
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
所需的输出2:
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
这是您想要的吗?(重命名列和索引名称)
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
基于您的要求。
所需的输出2:
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
和
所需的输出1:
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