gpt4 book ai didi

python - 如何将 pandas 系列的列值转换为 Python 列表?

转载 作者:行者123 更新时间:2023-12-01 23:43:55 25 4
gpt4 key购买 nike

我有一个 pandas 系列,打印后看起来像 -

0        NaN
1 20.307
2 -16.879
3 4.598
4 21.978
5 -12.913
dtype: float64

我想将第二列中的值转换为列表,使其看起来像 -

[0, 20.307, -16.879, 4.598, 21.978, -12.913]

我试过 - my_series.iloc[:, 1] 但我收到错误 -pandas.core.indexing.IndexingError:索引器太多有人可以帮忙吗?谢谢。

最佳答案

如果使用 iloc[:, 1] 它想要选择第二列,但在 Series 中没有列,因此引发错误。

如果想在不首先使用索引[1:]的情况下选择所有值:

L = my_series.iloc[1:].tolist()

或者通过Series.dropna删除缺失值:

L = my_series.dropna().tolist()

或者用Series.fillnaNaN替换为0 :

L = my_series.fillna(0).tolist()

关于python - 如何将 pandas 系列的列值转换为 Python 列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64567476/

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