Conversion between NumPy and Pandas is simple.
Let’s start with importing NumPy and Pandas, and then make a Pandas dataframe.
import numpy as np
import pandas as pd
df = pd.DataFrame()
names = ['Gandolf','Gimli','Frodo','Legolas','Bilbo']
types = ['Wizard','Dwarf','Hobbit','Elf','Hobbit']
magic = [10, 1, 4, 6, 4]
aggression = [7, 10, 2, 5, 1]
stealth = [8, 2, 5, 10, 5]
df['names'] = names
df['type'] = types
df['magic_power'] = magic
df['aggression'] = aggression
df['stealth'] = stealth
print (df)
OUT:
names type magic_power aggression stealth
0 Gandolf Wizard 10 7 8
1 Gimli Dwarf 1 10 2
2 Frodo Hobbit 4 2 5
3 Legolas Elf 6 5 10
4 Bilbo Hobbit 4 1 5
Converting from Pandas to NumPy
We will use the values method to convert from Pandas to NumPy. Notice that we loose our column headers when converting to a NumPy array, and the index filed (name) simply becomes the first column.
my_array = df.values
print (my_array)
OUT:
[['Gandolf' 'Wizard' 10 7 8]
['Gimli' 'Dwarf' 1 10 2]
['Frodo' 'Hobbit' 4 2 5]
['Legolas' 'Elf' 6 5 10]
['Bilbo' 'Hobbit' 4 1 5]]
Converting from NumPy to Pandas
We will use the dataframe method to convert from a NumPy array to a Pandas dataframe. A new index has been created, and columns have been given numerical headers.
my_new_df = pd.DataFrame(my_array)
print (my_new_df)
OUT:
0 1 2 3 4
0 Gandolf Wizard 10 7 8
1 Gimli Dwarf 1 10 2
2 Frodo Hobbit 4 2 5
3 Legolas Elf 6 5 10
4 Bilbo Hobbit 4 1 5
If we have column names, we can supply those to the dataframe during the conversion process. We pass a list to the dataframe method:
names = ['name','type','magic_power','aggression','strength']
my_new_df = pd.DataFrame(my_array, columns=names)
print(my_new_df)
OUT:
name type magic_power aggression strength
0 Gandolf Wizard 10 7 8
1 Gimli Dwarf 1 10 2
2 Frodo Hobbit 4 2 5
3 Legolas Elf 6 5 10
4 Bilbo Hobbit 4 1 5
And, as we have seen previously, we can set the index to a particular column:
my_new_df.set_index('name', inplace=True)
print (my_new_df)
OUT:
type magic_power aggression strength
name
Gandolf Wizard 10 7 8
Gimli Dwarf 1 10 2
Frodo Hobbit 4 2 5
Legolas Elf 6 5 10
Bilbo Hobbit 4 1 5
One thought on “24. Converting between NumPy and Pandas”