Formatting Numpy list
Question:
Good Morning, im relatively new in python and I need some help with formatting. I have a Numpy list with the following format:
[['SPY'], ['XLE'], ['XLF'], ['XLY'], ['GLD'], ['FXE']]
But I would like to convert it to a list like this:
['Games', 'Fin']
Sorry for the dumb question and thank you for your help 🙂
Answers:
You can use the flatten
method of the np.array
.
import numpy as np
a = np.array([['SPY'], ['XLE'], ['XLF'], ['XLY'], ['GLD'], ['FXE']])
print(a.flatten())
# ['SPY' 'XLE' 'XLF' 'XLY' 'GLD' 'FXE']
Source: numpy docs
Good Morning, im relatively new in python and I need some help with formatting. I have a Numpy list with the following format:
[['SPY'], ['XLE'], ['XLF'], ['XLY'], ['GLD'], ['FXE']]
But I would like to convert it to a list like this:
['Games', 'Fin']
Sorry for the dumb question and thank you for your help 🙂
You can use the flatten
method of the np.array
.
import numpy as np
a = np.array([['SPY'], ['XLE'], ['XLF'], ['XLY'], ['GLD'], ['FXE']])
print(a.flatten())
# ['SPY' 'XLE' 'XLF' 'XLY' 'GLD' 'FXE']
Source: numpy docs