从pandas数据框列获取列表
我有一个看起来像这样的Excel文档。
cluster load_date   budget  actual  fixed_priceA   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的内容-群集作为列表返回,因此我可以对其运行一个for循环,并为每个群集创建一个excel工作表。
还可以将整行的内容返回到列表吗?例如
list = [], list[column1] or list[df.ix(row1)]回答:
当您将Pandas
DataFrame列拉出时,它们就是Pandas系列,然后您可以调用x.tolist()将其转换为Python列表。另外,您也可以使用list(x)。
import pandas as pddata_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'>
以上是 从pandas数据框列获取列表 的全部内容, 来源链接: utcz.com/qa/425252.html

