223k views
4 votes
Take the number of index levels, num_li, and the number of column levels, num_l c, from the user. Take num_li strings, each representing labels in each index level starting from 0. Take num_lc strings, each representing labels in each column level starting from 0 . The strings contain labels (which are also strings) separated by a space. For example, if one of the input strings is "foo bar baz", it means that there are three labels: "foo", "bar", and "baz" at that level. b. (5 points) Create a DataFrame named df with num_li levels of index and num_lc levels of column, and the labels in each level of index and column are as specified by the input strings. Fill df with integers starting from 0 as shown in the example below. Print df. Suppose that the values of num_li and num_lc are 2, the strings for index labels are "white white red red" and "up down up down", and the strings for column labels are "pen pen paper paper" and "1 212 ", then the value of df should look like this:

User Aameer
by
7.7k points

1 Answer

3 votes

Final answer:

The question pertains to creating a multi-level index and column Data Frame using pandas in Python, using user-provided label strings and filling it with incremental integers.

Step-by-step explanation:

The task is to create a pandas DataFrame in Python, with a specific number of index levels (num_li) and column levels (num_lc). This DataFrame will have index and column labels defined by input strings provided by the user where labels are separated by spaces.

For example, if num_li is 2 and num_lc is also 2, and assuming the index labels are "white white red red" for the first level and "up down up down" for the second level, and the column labels are "pen pen paper paper" for the first level and "1 2 1 2" for the second level, the resulting Data Frame should be filled with integers starting at 0 and looking similar to the table provided in the question's example.

User Jeremiah N
by
7.5k points