python - 操纵 Pandas 数据框以显示所需的输出

标签 python pandas

我有以下 DataFrame 结构:

profile_id  user   birthday
123, 124    test1  day1
131, 132    test2  day2

我需要显示的是:

profile_id  user   birthday
123        test1   day1 
124        test1   day1
131        test2   day2
132        test2   day2

在 profile_id 列中,我有几个用逗号分隔的 ID,我需要遍历每个 ID。

最佳答案

这是一种方法

In [1127]: dfs = (df.profile_id.str.split(', ', expand=True).stack()
                   .reset_index(name='profile_id'))

In [1128]: df.loc[dfs.level_0].assign(profile_id=dfs.profile_id)
Out[1128]:
  profile_id   user birthday
0        123  test1     day1
0        123  test1     day1
1        124  test2     day2
1        124  test2     day2

关于python - 操纵 Pandas 数据框以显示所需的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52188203/

相关文章:

python - Pandas 查找日期频率

python - 使用 numpy/quantize 进行几何舍入?

python - dataframe.series 和 dataframe ['series' 之间有什么区别]?

python - pip:缺少分发规范。如何解决这个问题?

python - 处理 pandas 中的嵌套 json

pandas - 如何将多组列组合为 pandas 中的条形图

javascript - Chart.js 的 X 轴未采用类型 : 'time'

python - 如何使用 python 和 BeautifulSoup 添加 html 属性

python - 将曲线拟合到散点图的边界

python - 如何在一张图中绘制两个颜色相同但线条样式不同的 pandas DataFrame?