Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

python - convert a column in a dataframe into a list

myvar_list = df["myvar"].tolist()
Comment

how to get a dataframe column as a list

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:
{df}
")
print(f"column types:
{df.dtypes}")

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

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

print(f"
col_one_list:
{col_one_list}
type:{type(col_one_list)}")
print(f"
col_one_arr:
{col_one_arr}
type:{type(col_one_arr)}")
Comment

pandas dataframe lists as columns

In [8]: data = pd.DataFrame({'x': x, 'sin(x)': y})
In [9]: data
Out[9]: 
          x        sin(x)
0  0.000000  0.000000e+00
1  0.349066  3.420201e-01
2  0.698132  6.427876e-01
3  1.047198  8.660254e-01
4  1.396263  9.848078e-01
5  1.745329  9.848078e-01
6  2.094395  8.660254e-01
7  2.443461  6.427876e-01
8  2.792527  3.420201e-01
9  3.141593  1.224647e-16

[10 rows x 2 columns]
Comment

list to dataframe columns

import pandas as pd

lst = [1,2,3]
df = pd.DataFrame([lst])
df.columns =['col1','col2','col3']
df

to get this:

    col1    col2    col3
0   1       2       3
Comment

PREVIOUS NEXT
Code Example
Python :: python rotate list 
Python :: how to install django 
Python :: python convert string to list of dictionaries 
Python :: pandas df to list of dictionaries 
Python :: how to get the year and month in python 
Python :: discord.py setup_hook 
Python :: python regular expressions 
Python :: python sum of a subset 
Python :: pandas dataframe map 
Python :: append multiple values to 2d list python 
Python :: python venv usage 
Python :: python check if file is writable 
Python :: Code to implement iterative Binary Search 
Python :: python 2.7 venv 
Python :: print(int()) 
Python :: python override string class 
Python :: how to check if a string is lowercase in python 
Python :: raise 400 error python 
Python :: sum of multiples of 3 or 5 python 
Python :: how to add list as new row to pandas dataframe 
Python :: Python List count() example 
Python :: cv2 cuda support print 
Python :: exponent in python 
Python :: flask get uploaded file size 
Python :: upload file to s3 python 
Python :: colorgram in python 
Python :: download unsplash images script 
Python :: python milisegundos 
Python :: run all python files in a directory in bash 
Python :: Add label to histogram 
ADD CONTENT
Topic
Content
Source link
Name
6+4 =