本文介绍了要列出的Pandas DataFrame列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在根据另一列中满足的条件从一列中提取数据的子集.

I am pulling a subset of data from a column based on conditions in another column being met.

我可以获取正确的值,但是它在pandas.core.frame.DataFrame中.如何将其转换为列表?

I can get the correct values back but it is in pandas.core.frame.DataFrame. How do I convert that to list?

import pandas as pd

tst = pd.read_csv('C:\\SomeCSV.csv')

lookupValue = tst['SomeCol'] == "SomeValue"
ID = tst[lookupValue][['SomeCol']]
#How To convert ID to a list

推荐答案

您可以使用 Series.to_list 方法.

You can use the Series.to_list method.

例如:

import pandas as pd

df = pd.DataFrame({'a': [1, 3, 5, 7, 4, 5, 6, 4, 7, 8, 9],
                   'b': [3, 5, 6, 2, 4, 6, 7, 8, 7, 8, 9]})

print(df['a'].to_list())

输出:

[1, 3, 5, 7, 4, 5, 6, 4, 7, 8, 9]

要删除重复项,您可以执行以下操作之一:

To drop duplicates you can do one of the following:

>>> df['a'].drop_duplicates().to_list()
[1, 3, 5, 7, 4, 6, 8, 9]
>>> list(set(df['a'])) # as pointed out by EdChum
[1, 3, 4, 5, 6, 7, 8, 9]

这篇关于要列出的Pandas DataFrame列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 02:29