Python Pandas DataFrame: Create and Manipulate Data Tables
Pandas DataFrame is a powerful and versatile data structure in Python's pandas library, providing a two-dimensional, table-like representation of data. It's extensively used for data analysis and manipulation, making it a fundamental tool for data scientists and developers.
Syntax:
pd.DataFrame(data=None, index=None, columns=None, dtype=None, copy=False)
Parameters:
- data: Can be a list, dictionary, NumPy array, or another DataFrame. This is the actual data you want to store in the DataFrame.
- index: Specifies the labels for the rows. If not provided, a default integer index is generated.
- columns: Specifies the labels for the columns. If not provided, default column labels are assigned.
- dtype: Sets the data type for the columns. This is helpful for ensuring data consistency.
- copy: Determines whether to create a copy of the data or use a reference. By default, it's set to
False.
Example:
import pandas as pd
data = {'Name': ['John', 'Emily', 'Mike', 'Lisa'],
'Age': [25, 30, 35, 40],
'Gender': ['Male', 'Female', 'Male', 'Female']}
df = pd.DataFrame(data)
print(df)
Output:
Name Age Gender
0 John 25 Male
1 Emily 30 Female
2 Mike 35 Male
3 Lisa 40 Female
This code snippet demonstrates the creation of a DataFrame using a dictionary of data. The output displays a neatly formatted table with the specified data.
原文地址: https://www.cveoy.top/t/topic/gBuo 著作权归作者所有。请勿转载和采集!