Kursinhalt
Pandas: First Steps
Pandas: First Steps
What is a DataFrame?
A DataFrame is a core data structure in Pandas used for storing and manipulating Tabular Data.
It provides many useful methods and features for analyzing, filtering, and transforming the data efficiently.
The general syntax of the DataFrame
constructor is:
js
Here index
and column
are optional named parameters, which take up the default numerical values if not custom values are specified.
There are several ways to create a DataFrame:
Dictionary of Lists
One of the most common methods is to use a dictionary where the keys are column names and the values are lists of data.
import pandas as pd data = { 'Name': ['Alice', 'Bob', 'Charlie'], 'Age': [25, 30, 35], 'City': ['New York', 'Paris', 'London'] } df = pd.DataFrame(data) print(df)
List of Dictionaries
You can also pass a list of dictionaries, where each dictionary represents a row of data.
import pandas as pd data = [ {'Name': 'Alice', 'Age': 25, 'City': 'New York'}, {'Name': 'Bob', 'Age': 30, 'City': 'Paris'}, {'Name': 'Charlie', 'Age': 35, 'City': 'London'} ] df = pd.DataFrame(data) print(df)
2D List (List of Lists)
You can also use a list of lists (2D list). In this case you need to explicitly specify the column names in the DataFrame
constructor using the columns
named parameter:
import pandas as pd data = [ ['Alice', 25, 'New York'], ['Bob', 30, 'Paris'], ['Charlie', 35, 'London'] ] df = pd.DataFrame(data, columns=['Name', 'Age', 'City']) print(df)
1. What is a Pandas DataFrame?
2. Which of the following code snippets will create a DataFrame with columns "Name"
and "Age"
?
3. What does the columns
argument in pd.DataFrame()
do when using a 2D list?
Danke für Ihr Feedback!