Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to make an array from the values of a column in pandas while dropping the index

Tags:

arrays

pandas

I have a data frame about movies. One of the columns is titled 'genres'. I want to take all the string values from the genres column and convert it into an array. I am using the loc function to do this.

genres_list = df.loc[:,'genres'] this list has an index and I can not get rid of it using the .reset_index() function. this image is of the indexed list

like image 790
Azka Amir Avatar asked Oct 17 '25 07:10

Azka Amir


2 Answers

so the thing that worked for me is using the .values tool. In an example that looks like that:

>>> df = pd.DataFrame({"A": [1, 2], "B": [3, 4]})
>>> 
>>> list = df.loc[:,'A']
>>> list
0    1
1    2
Name: A, dtype: int64
>>> list = df.loc[:,'A'].values
>>> 
>>> list
array([1, 2])
>>> 

This still is a numpy array but can be converted with list.tolist()

Reference: https://www.codegrepper.com/code-examples/python/python+dataframe+to+array+without+index

like image 178
SebNik Avatar answered Oct 19 '25 20:10

SebNik


Easy:

>>> df = pd.DataFrame({"A": [1, 2], "B": [3, 4]})
>>> df['A'].to_list()
>>> [1, 2]
like image 38
phil Avatar answered Oct 19 '25 19:10

phil