Write a Pandas program to create and display a DataFrame from a specified dictionary data which has the index labels.
Example 1:
Input: {'Name': ['Tom', 'Jack', 'Steve', 'Ricky'],'Age': [28, 34, 29, 42]}, index=['rank1', 'rank2', 'rank3', 'rank4'] Output: DataFrame
Example 2:
Input: {'Country': ['USA', 'Canada', 'Germany', 'UK'],'Population': [328, 37, 83, 66]}, index=['rank1', 'rank2', 'rank3', 'rank4'] Output: DataFrame
Use pandas DataFrame function to create DataFrame from dictionary. Provide index as a separate argument.
import pandas as pd def create_df(dictionary, index): df = pd.DataFrame(dictionary, index=index) return df print(create_df({'Name': ['Tom', 'Jack', 'Steve', 'Ricky'],'Age': [28, 34, 29, 42]}, index=['rank1', 'rank2', 'rank3', 'rank4'])) print(create_df({'Country': ['USA', 'Canada', 'Germany', 'UK'],'Population': [328, 37, 83, 66]}, index=['rank1', 'rank2', 'rank3', 'rank4']))
Unlock AI & Data Science treasures. Log in!