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
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
Easy:
>>> df = pd.DataFrame({"A": [1, 2], "B": [3, 4]})
>>> df['A'].to_list()
>>> [1, 2]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With