Ask Your Question
3

How can a data-frame with multiple indices be created?

asked 2022-06-27 11:00:00 +0000

huitzilopochtli gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2022-10-21 05:00:00 +0000

ladyg gravatar image

A data-frame with multiple indices can be created in pandas by passing a list of column names to the index_col parameter of the read_csv method or by using the set_index method.

Here's an example of how to create a data-frame with multiple indices using the read_csv method:

import pandas as pd

# create a sample csv file
with open('data.csv', 'w') as f:
    f.write('col1,col2,col3\n')
    f.write('A,X,1\n')
    f.write('A,Y,2\n')
    f.write('B,X,3\n')
    f.write('B,Y,4\n')

# read data and set multiple indices
df = pd.read_csv('data.csv', index_col=['col1', 'col2'])

# print the data-frame
print(df)

This will output:

          col3
col1 col2     
A    X       1
     Y       2
B    X       3
     Y       4
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2022-06-27 11:00:00 +0000

Seen: 8 times

Last updated: Oct 21 '22