小编Håk*_*kon的帖子

根据现有列名在 Pandas DataFrame 中创建新列

我想解构 pandas DataFrame,使用列标题作为新的数据列,并创建一个包含行索引和列的所有组合的列表。展示比解释更容易:

index_col = ["store1", "store2", "store3"]
cols = ["January", "February", "March"]
values = [[2,3,4],[5,6,7],[8,9,10]]
df = pd.DataFrame(values, index=index_col, columns=cols)
Run Code Online (Sandbox Code Playgroud)

我希望从这个 DataFrame 中获得以下列表:

[['store1', 'January', 2],
 ['store1', 'February', 3],
 ['store1', 'March', 4],
 ['store2', 'January', 5],
 ['store2', 'February', 6],
 ['store2', 'March', 7],
 ['store3', 'January', 8],
 ['store3', 'February', 9],
 ['store3', 'March', 10]]
Run Code Online (Sandbox Code Playgroud)

有没有方便的方法来做到这一点?

python pandas

11
推荐指数
2
解决办法
2538
查看次数

标签 统计

pandas ×1

python ×1