DataFrame
is a 2D data structure that organizes data intorows
andcolumns
.- It is like a spreadsheet, but with more features.
DataFrames
can be created from a variety of data sources, such as CSV files, Excel files, and SQL databases.
- Cleaning and manipulating data
- Analyzing data trends
- Visualizing data using charts and graphs
Flexibility
: DataFrames can be used to store a variety of data types, including numeric, categorical, and string data.Scalability
: DataFrames can scale to handle large datasets.Performance
: DataFrames are optimized for performance, so you can quickly and easily perform complex data operations.Ease of use
: DataFrames are easy to use, even for beginners.
A. Using List
import pandas as pd
# Create a list of lists:
list_of_lists = [['Kirankumar', 27, 'Mumbai'], ['Sumit', 26, 'Patna'], ['Suraj', 27, 'Bangalore']]
# Convert the list of lists to a DataFrame:
df = pd.DataFrame(list_of_lists, columns=['Name', 'Age', 'City'])
# Print the DataFrame:
print(df)
B. Using zip
import pandas as pd
# Create lists of fields:
name = ['Kirankumar', 'Suraj', 'Sumit']
age = [27, 27, 26]
city = ['Mumbai', 'Bangalore', 'Patna']
# Combine the multiple lists into single list object:
df = pd.DataFrame(data=zip(name, age, city), columns=['Name', 'Age', 'City'])
# Print the DataFrame:
print(df)
C. Using List
of Dictionaries
import pandas as pd
# Create a list of dictionaries:
list_of_dictionaries = [{'Name': 'Kirankumar', 'Age': 27, 'Occupation': 'Data Scientist'}, {'Name': 'Suraj', 'Age': 27, 'Occupation': 'DevOps'}, {'Name': 'Sumit', 'Age': 26, 'Occupation': 'Python Developer'}]
# Convert the list of dictionaries to a DataFrame
df = pd.DataFrame(list_of_dictionaries)
# Print the DataFrame
print(df)