gpt4 book ai didi

python - 扁平化 pandas 数据框的最有效方法是什么?

转载 作者:行者123 更新时间:2023-11-30 22:47:16 26 4
gpt4 key购买 nike

我有一个很大的 pandas 数据框,有 8 列和几个 NaN 值:

0   1   2   3   4   5   6   7   8
1 Google, Inc. (Date 11/07/2016) NaN NaN NaN NaN NaN NaN NaN NaN
2 Apple Inc. (Date 07/01/2016) Amazon (Date 11/01/2016) NaN NaN NaN NaN NaN NaN NaN
3 IBM, Inc. (Date 11/08/2016) NaN NaN NaN NaN NaN NaN NaN NaN
4 Microsoft (Date 11/10/2016) Google, Inc. (Date 11/10/1990) Google, Inc. (Date 11/07/2016) Samsung (Date 05/02/2016) NaN NaN NaN NaN NaN

我怎样才能像这样压平它:

0   companies
1 Google, Inc. (Date 11/07/2016)
2 Apple Inc. (Date 07/01/2016)
3 Amazon (Date 11/01/2016)
4 IBM, Inc. (Date 11/08/2016)
5 Microsoft (Date 11/10/2016)
6 Google, Inc. (Date 11/10/1990)
7 Google, Inc. (Date 11/07/2016)
8 Samsung (Date 05/02/2016)

我读了docs并尝试过:

df.iloc[:,0]

问题是我丢失了其他列的信息和顺序。我知道如何在不丢失其他单元格中数据的情况下进行平坦化并进行排序?

最佳答案

您可以堆叠列并可以选择重置索引。默认情况下,堆栈会丢弃 NaN。

df.stack()
Out:
0 0 Google, Inc. (Date 11/07/2016)
1 0 Apple Inc. (Date 07/01/2016)
1 Amazon (Date 11/01/2016)
2 0 IBM, Inc. (Date 11/08/2016)
3 0 Microsoft (Date 11/10/2016)
1 Google, Inc. (Date 11/10/1990)
2 Google, Inc. (Date 11/07/2016)
3 Samsung (Date 05/02/2016)
dtype: object

df.stack().reset_index(drop=True)
Out:
0 Google, Inc. (Date 11/07/2016)
1 Apple Inc. (Date 07/01/2016)
2 Amazon (Date 11/01/2016)
3 IBM, Inc. (Date 11/08/2016)
4 Microsoft (Date 11/10/2016)
5 Google, Inc. (Date 11/10/1990)
6 Google, Inc. (Date 11/07/2016)
7 Samsung (Date 05/02/2016)
dtype: object

关于python - 扁平化 pandas 数据框的最有效方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40578788/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com