无法正确连接数据帧

2024-10-05 18:41:31 发布

您现在位置:Python中文网/ 问答频道 /正文

dfs是:

StockCode
84077K     32.694876
23005J     28.248135
85099BJ    24.581063
23084M     24.078340
85099FC    19.276526
Name: 127269, dtype: float64

其中127269是CustomerID

dfc是:

    CustomerID
0       127269

我正在尝试使用以下命令连接dfs和dfc:

final_frame = pd.concat([dfc, dfs], axis=1)

我得到的结果是:

         CustomerID      127269
     0     127269.0         NaN
84077K          NaN   32.694876

而我期望的结果是:

    CustomerID      StockCode
  0     127269         84077K

print (dfs) gives me:

StockCode
84077K    32.694876
Name: 127269, dtype: float64

print(dfc) gives me:
       CustomerID
0      127269

有人能建议怎样才能达到这个结果吗


Tags: name命令nanframefinalpdmeprint
2条回答

您的问题是dfs是一个系列,而不是一个数据帧,您需要用它的索引的第一个值来连接。所以你应该使用:

final_frame = pd.concat([dfc, dfs.reset_index().iloc[[0], [0]]], axis=1)

它应该给你预期的产出

我做了以下工作:

dfs = dfs.reset_index()

并得到以下输出:

dfs

StockCode   127269
0   84077K  32.694876

dfs1 = dfs['StockCode']

final_frame = pd.concat([dfc, dfs1], axis=1)

final_frame
    CustomerID  StockCode
0       127269  84077K

相关问题 更多 >