2024-04-25 17:24:26 发布
网友
我正在根据满足的另一列中的条件从列中提取数据的子集。
我可以得到正确的值,但它在pandas.core.frame.DataFrame中。如何将其转换为列表?
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
您可以使用pandas.Series.tolist
pandas.Series.tolist
例如:
import pandas as pd df = pd.DataFrame({'a':[1,2,3], 'b':[4,5,6]})
运行:
>>> df['a'].tolist()
你会得到
>>> [1, 2, 3]
我想澄清几件事:
pandas.Series.tolist()
pandas.Series.values.tolist()
tst[lookupValue][['SomeCol']]
tst[lookupValue]
[['SomeCol']]
tst[lookupValue]['SomeCol']
pandas.DataFrame.squeeze()
tst.loc[lookupValue, 'SomeCol']
ID = tst.loc[tst['SomeCol'] == 'SomeValue', 'SomeCol'].tolist()
演示代码:
import pandas as pd df = pd.DataFrame({'colA':[1,2,1], 'colB':[4,5,6]}) filter_value = 1 print "df" print df print type(df) rows_to_keep = df['colA'] == filter_value print "\ndf['colA'] == filter_value" print rows_to_keep print type(rows_to_keep) result = df[rows_to_keep]['colB'] print "\ndf[rows_to_keep]['colB']" print result print type(result) result = df[rows_to_keep][['colB']] print "\ndf[rows_to_keep][['colB']]" print result print type(result) result = df[rows_to_keep][['colB']].squeeze() print "\ndf[rows_to_keep][['colB']].squeeze()" print result print type(result) result = df.loc[rows_to_keep, 'colB'] print "\ndf.loc[rows_to_keep, 'colB']" print result print type(result) result = df.loc[df['colA'] == filter_value, 'colB'] print "\ndf.loc[df['colA'] == filter_value, 'colB']" print result print type(result) ID = df.loc[rows_to_keep, 'colB'].tolist() print "\ndf.loc[rows_to_keep, 'colB'].tolist()" print ID print type(ID) ID = df.loc[df['colA'] == filter_value, 'colB'].tolist() print "\ndf.loc[df['colA'] == filter_value, 'colB'].tolist()" print ID print type(ID)
结果:
df colA colB 0 1 4 1 2 5 2 1 6 <class 'pandas.core.frame.DataFrame'> df['colA'] == filter_value 0 True 1 False 2 True Name: colA, dtype: bool <class 'pandas.core.series.Series'> df[rows_to_keep]['colB'] 0 4 2 6 Name: colB, dtype: int64 <class 'pandas.core.series.Series'> df[rows_to_keep][['colB']] colB 0 4 2 6 <class 'pandas.core.frame.DataFrame'> df[rows_to_keep][['colB']].squeeze() 0 4 2 6 Name: colB, dtype: int64 <class 'pandas.core.series.Series'> df.loc[rows_to_keep, 'colB'] 0 4 2 6 Name: colB, dtype: int64 <class 'pandas.core.series.Series'> df.loc[df['colA'] == filter_value, 'colB'] 0 4 2 6 Name: colB, dtype: int64 <class 'pandas.core.series.Series'> df.loc[rows_to_keep, 'colB'].tolist() [4, 6] <type 'list'> df.loc[df['colA'] == filter_value, 'colB'].tolist() [4, 6] <type 'list'>
使用.values获取numpy.array,然后使用.tolist()获取列表。
.values
numpy.array
.tolist()
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]})
>>> df['a'].values.tolist() [1, 3, 5, 7, 4, 5, 6, 4, 7, 8, 9]
或者你可以用
>>> df['a'].tolist() [1, 3, 5, 7, 4, 5, 6, 4, 7, 8, 9]
要删除重复项,可以执行以下操作之一:
>>> df['a'].drop_duplicates().values.tolist() [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.Series.tolist
例如:
运行:
你会得到
我想澄清几件事:
pandas.Series.tolist()
。我不知道为什么最高投票的答案 首先使用pandas.Series.values.tolist()
,因为据我所知,它添加了语法/混乱,但没有额外的好处。tst[lookupValue][['SomeCol']]
是一个数据帧(如 问题),而不是一系列(如对问题的评论中所述)。这是因为tst[lookupValue]
是一个数据帧,用[['SomeCol']]
对它进行切片需要 列的列表(该列表的长度恰好为1),结果返回一个数据帧。如果你 移除额外的一组支架,如tst[lookupValue]['SomeCol']
,那么你只需要一个 列而不是列的列表,因此您将得到一个系列。pandas.Series.tolist()
,所以你应该 在这种情况下,一定要跳过第二组括号。仅供参考,如果你 以不易避免的单列数据帧结束 像这样,您可以使用pandas.DataFrame.squeeze()
将其转换为 一个系列。tst[lookupValue]['SomeCol']
通过 链式切片。它只切片一次以获得只有特定行的数据帧 向左,然后再次切片以获得特定列。你可以得到 因为你只是在读,不是在写,但是 正确的方法是tst.loc[lookupValue, 'SomeCol']
(返回一个序列)。ID = tst.loc[tst['SomeCol'] == 'SomeValue', 'SomeCol'].tolist()
演示代码:
结果:
使用
.values
获取numpy.array
,然后使用.tolist()
获取列表。例如:
结果:
或者你可以用
要删除重复项,可以执行以下操作之一:
相关问题 更多 >
编程相关推荐