需求场景

pandas dataframe 将一行按拆分成多行

拆分方法

方法一

df=df.drop('cont', axis=1).join(df['cont'].str.split('/', expand=True).stack().reset_index(level=1, drop=True).rename('tag'))

方法二

df=df['cont'].str.split('/', expand=True).stack().reset_index(level=0).set_index('level_0').rename(columns={0:'tag'}).join(df.drop('cont', axis=1))

方法三

df.assign(id = df.id.str.split('/')).explode('cont').drop_duplicates().reset_index(drop=True)

相关文章:

  • 2021-10-06
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-11-28
  • 2022-12-23
  • 2021-09-05
  • 2021-12-16
猜你喜欢
  • 2022-12-23
  • 2021-08-24
  • 2021-06-10
  • 2021-09-24
  • 2022-12-23
  • 2021-07-30
相关资源
相似解决方案