Python 如何向每行添加数据帧?
如果我有一个像这样的熊猫数据框:Python 如何向每行添加数据帧?,python,pandas,python-2.7,Python,Pandas,Python 2.7,如果我有一个像这样的熊猫数据框: 0 1 2 0 0 0 0 1 1 0 1 2 0 0 1 3 1 1 0 0 1 2 0 0 2 3 熊猫数据框如下: 0 1 2 0 0 0 0 1 1 0 1 2 0 0 1 3 1 1 0 0 1 2 0 0 2 3 如何将此数组
0 1 2
0 0 0 0
1 1 0 1
2 0 0 1
3 1 1 0
0 1 2
0 0 2 3
熊猫数据框如下:
0 1 2
0 0 0 0
1 1 0 1
2 0 0 1
3 1 1 0
0 1 2
0 0 2 3
如何将此数组连接到每一行,以获得如下新数据帧:
0 1 2 3 4 5
0 0 0 0 0 2 3
1 1 0 1 0 2 3
2 0 0 1 0 2 3
3 1 1 0 0 2 3
有一个删除的答案非常接近
# merge the data frame
df = pd.concat([df1, df2], axis=1, sort=False)
# rename dataframe to advoid duplications
df.columns = range(len(df.columns))
# fill na's in the columns of df2
df[-len(df2.columns):].ffill(inplace=True)
有一个删除的答案非常接近
# merge the data frame
df = pd.concat([df1, df2], axis=1, sort=False)
# rename dataframe to advoid duplications
df.columns = range(len(df.columns))
# fill na's in the columns of df2
df[-len(df2.columns):].ffill(inplace=True)
可以使用dict分配多个静态值:
df1.columns = ['3', '4', '5']
df = df.assign(**df1.to_dict('index')[0])
# If need to be int names: df.columns = df.columns.astype(int)
0 1 2 3 4 5
0 0 0 0 0 2 3
1 1 0 1 0 2 3
2 0 0 1 0 2 3
3 1 1 0 0 2 3
可以使用dict分配多个静态值:
df1.columns = ['3', '4', '5']
df = df.assign(**df1.to_dict('index')[0])
# If need to be int names: df.columns = df.columns.astype(int)
0 1 2 3 4 5
0 0 0 0 0 2 3
1 1 0 1 0 2 3
2 0 0 1 0 2 3
3 1 1 0 0 2 3
可能的重复可能的重复