簡體   English   中英

如何將熊貓數據框添加到每一行?

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

如果我有這樣的熊貓數據框:

     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 

我如何將這個數組連接到每一行,這樣我就得到一個新的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

刪除的答案非常接近。

# 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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM