我有一个dataframe df从一个Excel文档导入,就像这样:

cluster load_date   budget  actual  fixed_price
A   1/1/2014    1000    4000    Y
A   2/1/2014    12000   10000   Y
A   3/1/2014    36000   2000    Y
B   4/1/2014    15000   10000   N
B   4/1/2014    12000   11500   N
B   4/1/2014    90000   11000   N
C   7/1/2014    22000   18000   N
C   8/1/2014    30000   28960   N
C   9/1/2014    53000   51200   N

我希望能够返回列1 df['cluster']的内容作为列表,这样我就可以在上面运行for循环,并为每个集群创建一个Excel工作表。

是否也可以将整个列或行的内容返回到列表中?如。

list = [], list[column1] or list[df.ix(row1)]

当前回答

TL;DR:使用.tolist()。不要使用list()

如果我们看一下.tolist()的源代码,在底层,list()函数是在数据框架中的底层数据上调用的,因此两者应该产生相同的输出。

然而,看起来tolist()针对Python标量的列进行了优化,因为我发现在列上调用list()比调用tolist()慢10倍。为了记录,我试图将一个非常大的dataframe中的一列json字符串转换为一个列表,list()正在花费它的甜蜜时间。这启发了我去测试这两个方法的运行时。

仅供参考,不需要调用.to_numpy()或获取.values属性,因为数据帧列/系列对象已经实现了.tolist()方法。此外,由于numpy数组的存储方式,list()和tolist()将为数值列提供不同类型的标量(至少)。例如,

type(list(df['budget'].values)[0])     # numpy.int64
type(df['budget'].values.tolist()[0])  # int

下面的perfplot显示了这两种方法在不同pandas dtype Series对象上的运行时差异。基本上,它显示了以下两个方法之间的运行时差异:

list(df['some_col'])      # list()
df['some_col'].tolist()   # .tolist()

如您所见,无论列/Series的大小,对于数值和对象dtype列/Series, .tolist()方法都比list()快得多。这里没有包括浮点型和bool型dtype列的图,但它们与这里显示的int型dtype列的图非常相似。另外,包含列表的对象dtype列的图与这里显示的字符串列的图非常相似。扩展类型,如'Int64Dtype', 'StringDtype', 'Float64Dtype'等也显示了类似的模式。

另一方面,对于datetime、timedelta和Categorical dtype列,这两种方法实际上没有什么区别。

用于生成上述图表的代码:

from perfplot import plot
kernels = [lambda s: list(s), lambda s: s.tolist()]
labels = ['list()', '.tolist()']
n_range = [2**k for k in range(4, 20)]
xlabel = 'Rows in DataFrame'
eq_chk = lambda x,y: all([x,y])

numeric = lambda n: pd.Series(range(5)).repeat(n)
string = lambda n: pd.Series(['some word', 'another word', 'a word']).repeat(n)
datetime = lambda n: pd.to_datetime(pd.Series(['2012-05-14', '2046-12-31'])).repeat(n)
timedelta = lambda n: pd.to_timedelta(pd.Series([1,2]), unit='D').repeat(n)
categorical = lambda n: pd.Series(pd.Categorical([1, 2, 3, 1, 2, 3])).repeat(n)

for n, f in [('Numeric', numeric), ('Object dtype', string), 
             ('Datetime', datetime), ('Timedelta', timedelta), 
             ('Categorical', categorical)]:
    plot(setup=f, kernels=kernels, labels=labels, n_range=n_range, 
         xlabel=xlabel, title=f'{n} column', equality_check=eq_chk);

其他回答

当您将Pandas DataFrame列取出时,它们就是Pandas Series,然后您可以调用x.tolist()将它们转换为Python列表。或者使用list(x)强制转换。

import pandas as pd

data_dict = {'one': pd.Series([1, 2, 3], index=['a', 'b', 'c']),
             'two': pd.Series([1, 2, 3, 4], index=['a', 'b', 'c', 'd'])}

df = pd.DataFrame(data_dict)

print(f"DataFrame:\n{df}\n")
print(f"column types:\n{df.dtypes}")

col_one_list = df['one'].tolist()

col_one_arr = df['one'].to_numpy()

print(f"\ncol_one_list:\n{col_one_list}\ntype:{type(col_one_list)}")
print(f"\ncol_one_arr:\n{col_one_arr}\ntype:{type(col_one_arr)}")

输出:

DataFrame:
   one  two
a  1.0    1
b  2.0    2
c  3.0    3
d  NaN    4

column types:
one    float64
two      int64
dtype: object

col_one_list:
[1.0, 2.0, 3.0, nan]
type:<class 'list'>

col_one_arr:
[ 1.  2.  3. nan]
type:<class 'numpy.ndarray'>

返回一个numpy数组:

arr = df["cluster"].to_numpy()

返回唯一值的numpy数组:

unique_arr = df["cluster"].unique()

你也可以使用numpy来获取唯一的值,尽管这两种方法之间存在差异:

arr = df["cluster"].to_numpy()
unique_arr = np.unique(arr)

假设读取excel表格后的dataframe的名称是df,取一个空列表(例如dataList),逐行迭代dataframe,并像-一样添加到空列表中

dataList = [] #empty list
for index, row in df.iterrows(): 
    mylist = [row.cluster, row.load_date, row.budget, row.actual, row.fixed_price]
    dataList.append(mylist)

Or,

dataList = [] #empty list
for row in df.itertuples(): 
    mylist = [row.cluster, row.load_date, row.budget, row.actual, row.fixed_price]
    dataList.append(mylist)

不,如果你打印dataList,你会得到每一行作为一个列表在dataList。

TL;DR:使用.tolist()。不要使用list()

如果我们看一下.tolist()的源代码,在底层,list()函数是在数据框架中的底层数据上调用的,因此两者应该产生相同的输出。

然而,看起来tolist()针对Python标量的列进行了优化,因为我发现在列上调用list()比调用tolist()慢10倍。为了记录,我试图将一个非常大的dataframe中的一列json字符串转换为一个列表,list()正在花费它的甜蜜时间。这启发了我去测试这两个方法的运行时。

仅供参考,不需要调用.to_numpy()或获取.values属性,因为数据帧列/系列对象已经实现了.tolist()方法。此外,由于numpy数组的存储方式,list()和tolist()将为数值列提供不同类型的标量(至少)。例如,

type(list(df['budget'].values)[0])     # numpy.int64
type(df['budget'].values.tolist()[0])  # int

下面的perfplot显示了这两种方法在不同pandas dtype Series对象上的运行时差异。基本上,它显示了以下两个方法之间的运行时差异:

list(df['some_col'])      # list()
df['some_col'].tolist()   # .tolist()

如您所见,无论列/Series的大小,对于数值和对象dtype列/Series, .tolist()方法都比list()快得多。这里没有包括浮点型和bool型dtype列的图,但它们与这里显示的int型dtype列的图非常相似。另外,包含列表的对象dtype列的图与这里显示的字符串列的图非常相似。扩展类型,如'Int64Dtype', 'StringDtype', 'Float64Dtype'等也显示了类似的模式。

另一方面,对于datetime、timedelta和Categorical dtype列,这两种方法实际上没有什么区别。

用于生成上述图表的代码:

from perfplot import plot
kernels = [lambda s: list(s), lambda s: s.tolist()]
labels = ['list()', '.tolist()']
n_range = [2**k for k in range(4, 20)]
xlabel = 'Rows in DataFrame'
eq_chk = lambda x,y: all([x,y])

numeric = lambda n: pd.Series(range(5)).repeat(n)
string = lambda n: pd.Series(['some word', 'another word', 'a word']).repeat(n)
datetime = lambda n: pd.to_datetime(pd.Series(['2012-05-14', '2046-12-31'])).repeat(n)
timedelta = lambda n: pd.to_timedelta(pd.Series([1,2]), unit='D').repeat(n)
categorical = lambda n: pd.Series(pd.Categorical([1, 2, 3, 1, 2, 3])).repeat(n)

for n, f in [('Numeric', numeric), ('Object dtype', string), 
             ('Datetime', datetime), ('Timedelta', timedelta), 
             ('Categorical', categorical)]:
    plot(setup=f, kernels=kernels, labels=labels, n_range=n_range, 
         xlabel=xlabel, title=f'{n} column', equality_check=eq_chk);

下面是简单的一行代码:

load_date’(df)名单

更新:toList()不起作用。它应该是小写的。tolist()