[Python Cookbook]Pandas: How to increase columns for DataFrame?Join/Concat

醉酒当歌 提交于 2020-02-28 05:26:50

 

1. Combine Two Series

series1=pd.Series([1,2,3],name='s1')
series2=pd.Series([4,5,6],name='s2')
df = pd.concat([series1, series2], axis=1)

Out:

series1=pd.Series([1,2,3],index=['a','b','c'],name='s1')
series2=pd.Series([4,5,6],index=['a','b','d'],name='s2')
df = pd.concat([series1, series2], axis=1)

Out:

 

 

 Note: Two series must have names.

 

2. Add a series to a data frame

df=pd.DataFrame([1,2,3],index=['a','b','c'],columns=['s1'])
s2=pd.Series([4,5,6],index=['a','b','d'],name='s2')
df['s2']=s2

Out:

 

This method is equivalant to left join:

d2.join(s2,how='left',inplace=True)

To get the same result as Part 1, we can use outer join:

d2.join(s2,how='outer',inplace=True)

 

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