Get list from pandas dataframe column or row?

yoshiserry

I have a dataframe df imported from an Excel document like this:

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

I want to be able to return the contents of column 1 df['cluster'] as a list, so I can run a for-loop over it, and create an Excel worksheet for every cluster.

Is it also possible to return the contents of a whole column or row to a list? e.g.

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

Pandas DataFrame columns are Pandas Series when you pull them out, which you can then call x.tolist() on to turn them into a Python list. Alternatively you cast it with 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)}")

Output:

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'>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Get list from pandas DataFrame column headers

From Java

get list from pandas dataframe column

From Dev

Get value from dataframe based on column and row

From Dev

Column to row in pandas dataframe

From Dev

Get particular row as series from pandas dataframe

From Dev

Get cell value from a pandas DataFrame row

From Dev

Build list from column names Pandas DataFrame

From Dev

Pandas DataFrame get substrings from column

From Dev

Selecting pandas dataframe column by row-specific list

From Dev

Pandas: Get out values from a list in a DataFrame

From Dev

Get values from a list of dictionaries in a Pandas Dataframe

From Dev

Get column and row index pairs of Pandas DataFrame matching some criteria

From Dev

Get the row and column labels for selected values in a Pandas dataframe

From Dev

How to get column name for second largest row value in pandas DataFrame

From Dev

Get Column and Row Index for Highest Value in Dataframe Pandas

From Dev

Get column and row index pairs of Pandas DataFrame matching some criteria

From Dev

Get the row and column labels for selected values in a Pandas dataframe

From Dev

Pandas DataFrame: create a column from a list inside a column

From Dev

Pandas: access data from dataframe by row and column number

From Dev

How to construct pandas DataFrame from dictionary with key as column and row index

From Dev

Pandas Dataframe replace Nan from a row when a column value matches

From Dev

How to prevent Pandas Dataframe from repeating column names at every row?

From Java

Get row-index values of Pandas DataFrame as list?

From Dev

Create a list including row name, column name and the value from dataframe

From Dev

Get the string from the DataFrame column an assign to the other column in pandas

From Java

Construct pandas DataFrame from list of tuples of (row,col,values)

From Dev

Construct Pandas DataFrame from dictionary in form {index: list of row values}

From Dev

Occurence frequency from a list against each row in Pandas dataframe

From Dev

Pandas Dataframe row by row fill new column

Related Related

  1. 1

    Get list from pandas DataFrame column headers

  2. 2

    get list from pandas dataframe column

  3. 3

    Get value from dataframe based on column and row

  4. 4

    Column to row in pandas dataframe

  5. 5

    Get particular row as series from pandas dataframe

  6. 6

    Get cell value from a pandas DataFrame row

  7. 7

    Build list from column names Pandas DataFrame

  8. 8

    Pandas DataFrame get substrings from column

  9. 9

    Selecting pandas dataframe column by row-specific list

  10. 10

    Pandas: Get out values from a list in a DataFrame

  11. 11

    Get values from a list of dictionaries in a Pandas Dataframe

  12. 12

    Get column and row index pairs of Pandas DataFrame matching some criteria

  13. 13

    Get the row and column labels for selected values in a Pandas dataframe

  14. 14

    How to get column name for second largest row value in pandas DataFrame

  15. 15

    Get Column and Row Index for Highest Value in Dataframe Pandas

  16. 16

    Get column and row index pairs of Pandas DataFrame matching some criteria

  17. 17

    Get the row and column labels for selected values in a Pandas dataframe

  18. 18

    Pandas DataFrame: create a column from a list inside a column

  19. 19

    Pandas: access data from dataframe by row and column number

  20. 20

    How to construct pandas DataFrame from dictionary with key as column and row index

  21. 21

    Pandas Dataframe replace Nan from a row when a column value matches

  22. 22

    How to prevent Pandas Dataframe from repeating column names at every row?

  23. 23

    Get row-index values of Pandas DataFrame as list?

  24. 24

    Create a list including row name, column name and the value from dataframe

  25. 25

    Get the string from the DataFrame column an assign to the other column in pandas

  26. 26

    Construct pandas DataFrame from list of tuples of (row,col,values)

  27. 27

    Construct Pandas DataFrame from dictionary in form {index: list of row values}

  28. 28

    Occurence frequency from a list against each row in Pandas dataframe

  29. 29

    Pandas Dataframe row by row fill new column

HotTag

Archive