Pyt*_*ous 11 python join dataframe pandas
有没有办法直接将系列加入DataFrame?
连接将位于数据帧的字段和系列的索引上.
我发现的唯一方法是首先将系列转换为数据帧,如下面的代码所示.
import numpy as np
import pandas as pd
df = pd.DataFrame()
df['a'] = np.arange(0, 4)
df['b'] = np.arange(100, 104)
s = pd.Series(data=np.arange(100, 103))
# this doesn't work
# myjoin = pd.merge(df, s, how='left', left_on='a', right_index=True)
# this does
s = s.reset_index()
# s becomes a Dataframe
# note you cannot reset the index of a series inplace
myjoin = pd.merge(df, s, how='left', left_on='a', right_on='index')
print myjoin
Run Code Online (Sandbox Code Playgroud)
小智 6
我想http://pandas.pydata.org/pandas-docs/stable/ generated/pandas.concat.html 可能会有所帮助。
例如内/外连接。
pd.concat((df,s), axis=1)
Out[26]:
a b 0
0 0 100 100
1 1 101 101
2 2 102 102
3 3 103 NaN
In [27]: pd.concat((df,s), axis=1, join='inner')
Out[27]:
a b 0
0 0 100 100
1 1 101 101
2 2 102 102
Run Code Online (Sandbox Code Playgroud)
Ale*_*lex -1
尝试连接():
import numpy as np
import pandas as pd
df= pd.DataFrame()
df['a']= np.arange(0,4)
df['b']= np.arange(100,104)
s =pd.Series(data = np.arange(100,103))
new_df = pd.concat((df, s), axis=1)
print new_df
Run Code Online (Sandbox Code Playgroud)
这打印:
a b 0
0 0 100 100
1 1 101 101
2 2 102 102
3 3 103 NaN
Run Code Online (Sandbox Code Playgroud)