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
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
Asked: 2022-06-27 11:00:00 +0000
Seen: 8 times
Last updated: Oct 21 '22
What is the process to incorporate multiple inheritance using OpenZeppelin upgradeable contracts?
Can the SequentialChain of Langchain handle multiple inputs?
What is the method to test multiple values that are returned using Jest?
How can a single writer and multiple readers achieve concurrency with only one write?
On occasion, when executing a test suite for multiple classes, the act of mocking is omitted.
How can I search multiple databases on the local host?
How to connect or associate multiple files in Visual Studio Code on Ubuntu OS?
What is the method to combine multiple rows of a dataframe into a single value?
Can you whitelist multiple domains using command prompt for CORS Anywhere?