Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

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

get dataframe column into a list

df_gearME = pd.read_excel('Gear M&Es.xlsx')
df_gearME['ColA'].to_list()
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

pandas read columns as list

from ast import literal_eval


df.Col3 = df.Col3.apply(literal_eval)
print(df.Col3[0][0])
Proj1
Comment

PREVIOUS NEXT
Code Example
Python :: python read line into list 
Python :: how to make a for loop increment by 2 in python 
Python :: pandas rename multiple columns 
Python :: flask server not reloading 
Python :: flask get ip of user 
Python :: python turn true or false into 0 or 1 
Python :: python pil to greyscale 
Python :: how to import model_to_dict 
Python :: timeit jupyter 
Python :: python check folder exists 
Python :: create virtual env 
Python :: plot histogram in seaborn 
Python :: discord py get channel id by name 
Python :: dataframe column data type 
Python :: python import beautifulsoup 
Python :: lecture de fichier python 
Python :: find order of characters python 
Python :: list to sentence python 
Python :: Math Module log() Function in python 
Python :: find record where dataframe column value contains 
Python :: flask read form data 
Python :: how to delete a csv file in python 
Python :: how to do element wise multiplication in numpy 
Python :: set cookie in chrome 
Python :: read json file python 
Python :: calcutalte average python 
Python :: ImportError: No module named flask 
Python :: networkx path between two nodes 
Python :: search google images python 
Python :: left join outer apply 
ADD CONTENT
Topic
Content
Source link
Name
8+5 =