11-pandas拼接

余生颓废 提交于 2020-03-09 00:52:38
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"]))

  

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