How to turn a table into an array in python
To turn a table into an array in Python, you can use the pandas
library. Here's an example:
import pandas as pd
# Create a table
table = [["apple", "banana", "cherry"],
["dog", "cat", "bird"],
["red", "yellow", "blue"]]
# Convert the table to a DataFrame
df = pd.DataFrame(table)
# Convert the DataFrame to an array
arr = df.values
print(arr)
The output will be:
[['apple' 'banana' 'cherry']
['dog' 'cat' 'bird']
['red' 'yellow' 'blue']]
Alternatively, you can use the numpy
library to convert a list of lists to an array. Here's an example:
import numpy as np
# Create a list of lists
table = [["apple", "banana", "cherry"],
["dog", "cat", "bird"],
["red", "yellow", "blue"]]
# Convert the list of lists to an array
arr = np.array(table)
print(arr)
The output will be the same as before. Note that using pandas
is generally easier and more flexible than using numpy
, but numpy
can be more efficient for certain types of operations.