pandas DataFrame set_index

set_index sets one column to index.

import pandas

people = [
    ('Einstein', 1879, 'physics'),
    ('Bohr', 1885, 'physics'),
    ('Weil', 1912, 'mathematics'),
    ('Keynes', 1883, 'economics'),
]

df = pandas.DataFrame(people, columns=['name', 'born', 'field'])

df2 = df.set_index('name')

print(df)
#        name  born        field
# 0  Einstein  1879      physics
# 1      Bohr  1885      physics
# 2      Weil  1912  mathematics
# 3    Keynes  1883    economics

print(df2)
#           born        field
# name
# Einstein  1879      physics
# Bohr      1885      physics
# Weil      1912  mathematics
# Keynes    1883    economics

Comments

Powered by Markdown

More