Python Dataframe: A Comprehensive Guide with Examples
A dataframe in Python is a two-dimensional labeled data structure with columns of potentially different types. It's similar to a spreadsheet or SQL table, or a dictionary of Series objects. Dataframes are one of the most commonly used data structures in data analysis and data science.
You can create a dataframe using various methods such as reading data from a CSV file, constructing a dataframe from a dictionary, or using the pandas library.
Example:
import pandas as pd
# Create a dataframe from a dictionary
data = {'Name': ['John', 'Jack', 'Mary', 'Anna'],
'Age': [25, 30, 28, 32],
'Gender': ['Male', 'Male', 'Female', 'Female']}
df = pd.DataFrame(data)
# Display the dataframe
print(df)
Output:
Name Age Gender
0 John 25 Male
1 Jack 30 Male
2 Mary 28 Female
3 Anna 32 Female
In this example, we created a dataframe from a dictionary containing the name, age, and gender of four individuals. We then used the pandas library to create a dataframe object and displayed it.
原文地址: https://www.cveoy.top/t/topic/nwx0 著作权归作者所有。请勿转载和采集!