码迷,mamicode.com
首页 > 其他好文 > 详细

11-pandas拼接

时间:2020-03-09 01:18:23      阅读:64      评论:0      收藏:0      [点我收藏+]

标签:cat   for   axis   python   nump   get   data   dataframe   random   

import numpy as np
import pandas as pd
x=np.array([1,2,3]).reshape(1,3)#调节形状为二维数组
y=np.array([4,5,6]).reshape(1,3)
z=np.array([7,8,9]).reshape(1,3)
print(np.concatenate([x,y,z]))#numpy二维数组的拼接
arr1=np.random.randn(4,4)
np.concatenate([arr1,arr1])#横着拼接
np.concatenate([arr1,arr1],axis=1)#竖着拼接
def make_dataFrame(cols,ind):
    data={c:[str(c)+str(i) for i in ind] for c in cols}
    return pd.DataFrame(data,ind)
print(make_dataFrame("abc",range(3)))

df1=make_dataFrame("AB",[1,2])
df2=make_dataFrame("AB",[3,4])
print(pd.concat([df1,df2]))
print(pd.concat([df1,df2],ignore_index=True))#覆盖原索引,重建索引
print(pd.concat([df1,df2],keys=["M","N"]))#增加索引

s1=pd.Series(["a","b","c"],index=[1,2,3])
s2=pd.Series(["e","f","g"],index=[4,5,6])
print(pd.concat([s1,s2]))

  

import numpy as np
import pandas as pd
import tushare as ts
stock600848=ts.get_hist_data(‘600848‘)
stock600898=ts.get_hist_data(‘600898‘)

stock600898price=stock600898[["open","high","close","low"]].head(5)
stock600848price=stock600848[["open","high","close","low"]].head(5)
print(pd.concat([stock600898price,stock600848price],keys=["600848","600898"]))

  

11-pandas拼接

标签:cat   for   axis   python   nump   get   data   dataframe   random   

原文地址:https://www.cnblogs.com/wcyMiracle/p/12446118.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!