Convert NumPy array "ndarray" to lilt in Python [tolist ()]

I extracted the value from csv with Python, and when I tried to turn it with the for statement, the extracted data was ** ndarray **, which was a version of the array without commas (,), so I had a hard time. => I solved it, so make a note

** Extraction destination csv **

sample.csv


sample_name,sample_colomn
aaaaaaaaaaa,bbbbbbbbbbbbb
ccccccccccc,ddddddddddddd

** Logic for extraction **

sample.py



import pandas as pd
import numpy as np

sample_data = pd.read_csv('sample.csv',index_col='sample_colomn')
sample_colomn = sample_data.index.values
print(sample_colomn)

# ['bbbbbbbbbbbbb' 'ddddddddddddd']

When I tried to turn ['bbbbbbbbbbbbb''ddddddddddddd'] with a for statement, I got angry with Type Error: only integer scalar arrays can be converted to a scalar index.

Countermeasures

Use ** tolist () ** to convert a version of an array without a comma (,) to a regular array (list)

sample.py



import pandas as pd
import numpy as np

sample_data = pd.read_csv('sample.csv',index_col='sample_colomn')
sample_colomn = sample_data.index.values
print(sample_colomn.tolist())

# ['bbbbbbbbbbbbb', 'ddddddddddddd']

that's all!

reference https://note.nkmk.me/python-numpy-list/

Recommended Posts

Convert NumPy array "ndarray" to lilt in Python [tolist ()]
Convert numpy int64 to python int
Subscript access to python numpy array
Convert markdown to PDF in Python
[Introduction to Python] <numpy ndarray> [edit: 2020/02/22]
Convert psd file to png in Python
numpy: I want to convert a single type ndarray to a structured array
Read big endian binary in Python and convert it to ndarray
Convert from Markdown to HTML in Python
Convert absolute URLs to relative URLs in Python
How to sort by specifying a column in the Python Numpy array.
Convert FBX files to ASCII <-> BINARY in Python
Convert PDFs to images in bulk with Python
Convert exponential notation float to str in Python
Convert cubic mesh code to WKT in Python
python numpy array calculation
Convert dict to array
Convert timezoned date and time to Unixtime in Python2.7
Various ways to extract columns in a NumPy array
Convert CIDR notation netmask to dotted decimal notation in Python
How to convert floating point numbers to binary numbers in Python
Convert the image in .zip to PDF with Python
Convert callback-style asynchronous API to async / await in Python
Convert / return class object to JSON format in Python
[Python] Created a method to convert radix in 1 second
Convert Webpay Entity type to Dict type (recursively in Python)
Convert elements of numpy array from float to int
[python] Convert date to string
Quicksort an array in Python 3
To flush stdout in Python
Empty multidimensional array in python
Login to website in Python
Matrix multiplication in python numpy
Convert Scratch project to Python
[Python] Convert Shift_JIS to UTF-8
Create a python numpy array
Convert CIDR notation in Python
Speech to speech in python [text to speech]
How to develop in Python
Convert python 3.x code to python 2.x
Post to Slack in Python
How to slice a block multiple array from a multiple array in Python
How to swap elements in an array in Python, and how to reverse an array.
Convert images passed to Jason Statham-like in Python to ASCII art
I want to align the significant figures in the Numpy array
Convert Excel file to text in Python for diff purposes
[Python] How to do PCA in Python
How to make a string into an array or an array into a string in Python
How to collect images in Python
Convert files written in python etc. to pdf with syntax highlighting
Put python, numpy, opencv3 in ubuntu14
How to use SQLite in Python
Self-organizing map in Python NumPy version
Workflow to convert formula (image) to python
In the python command python points to python3.8
Convert list to DataFrame with python
Try to calculate Trace in Python
How to convert 0.5 to 1056964608 in one shot
Python> list> Convert double list to single list
How to use Mysql in python
Python application: Numpy Part 3: Double array