【发布时间】:2022-09-28 07:50:56
【问题描述】:
我有以下数据框:
df1 = pd.DataFrame({\'ID\' : [\'T1002.\', \'T5006.\', \'T5007.\'],
\'Parent\': [\'Stay home.\', \"Stay home.\",\"Stay home.\"],
\'Child\' : [\'2Severe weather.\', \"5847.\", \"Severe weather.\"]})
ID Parent Child
0 T1002. Stay home. 2Severe weather.
1 T5006. Stay home. 5847.
2 T5007. Stay home. Severe weather.
我想将两列合二为一,并将列名称添加到行中。我还希望列名以粗体显示。
预期结果:(我不能将列名 ID 等加粗)
Joined_columns()
0 ID: T1002. Parent: Stay home. Child: 2Severe weather.
1 ID: T5006. Parent: Stay home. Child: 5847.
2 ID: T5007. Parent: Stay home. Child: Severe weather.
连接是通过以下代码完成的:
df1_final=df1.stack().groupby(level=0).apply(\' \'.join).to_frame(0)
但我不确定如何走到最后。有任何想法吗
标签: python python-3.x pandas dataframe join