简体   繁体   English

如何将熊猫数据框添加到每一行?

[英]How to add pandas data frame to each row?

If I have a pandas data frame like this: 如果我有这样的熊猫数据框:

     0   1   2 
 0   0   0   0
 1   1   0   1
 2   0   0   1
 3   1   1   0

and an pandas data frame like this: 和这样的熊猫数据框:

     0   1   2
 0   0   2   3 

How do I concatenate this array to each row such that I get a new pandas data frame like this: 我如何将这个数组连接到每一行,这样我就得到一个新的pandas数据框,如下所示:

     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

There was a deleted answer which was very closed. 删除的答案非常接近。

# 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)

Can assign multiple static values with a dict: 可以使用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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM