Loading Data into Pandas
AI in Action
import pandas as pd
df = pd.read_csv("passengers.csv")
print(df.head())
Reading Data from Files
Datasets are typically too large to be typed in manually or stored directly in code. That's why they are kept in external files on your computer or in online storage. To work with them in pandas, you first need to load the data into memory.
Most commonly, you will work with CSV files - tables where values are separated by commas. These files can be read with the pd.read_csv() function:
import pandas as pd
df = pd.read_csv("passengers.csv")
print(df.head())
Pandas can read many different data sources. You can explore all available options in the official documentation.
Reading Remote Files
If you want, you can also load files directly from the web. Just pass the URL instead of a file path.
1234import pandas as pd df = pd.read_csv("https://staging-content-media-cdn.codefinity.com/courses/64641555-cae4-4cd0-8d29-807aeb6bc0c4/datasets/passengers.csv") print(df.head())
Adjusting Parameters
Even in simple formats like CSV, there is room for different settings. You may want to add or remove a header, replace a comma with another separator, or handle unusual encodings. All this can be configured when loading the file:
import pandas as pd
# Change separator into a semicolon
df1 = pd.read_csv("data.csv", sep=";")
# Specify encoding type
df2 = pd.read_csv("data.csv", encoding="latin1")
# Read file without headers
df3 = pd.read_csv("data.csv", header=None)
# Assign custom column names
df4 = pd.read_csv("data.csv", names=["Date", "Product", "Price"])
1. Which pandas function loads data from a CSV file?
2. How do you read a tab-separated file named data.tsv? (Tab character is \t)
Obrigado pelo seu feedback!
Pergunte à IA
Pergunte à IA
Pergunte o que quiser ou experimente uma das perguntas sugeridas para iniciar nosso bate-papo
Awesome!
Completion rate improved to 10
Loading Data into Pandas
Deslize para mostrar o menu
AI in Action
import pandas as pd
df = pd.read_csv("passengers.csv")
print(df.head())
Reading Data from Files
Datasets are typically too large to be typed in manually or stored directly in code. That's why they are kept in external files on your computer or in online storage. To work with them in pandas, you first need to load the data into memory.
Most commonly, you will work with CSV files - tables where values are separated by commas. These files can be read with the pd.read_csv() function:
import pandas as pd
df = pd.read_csv("passengers.csv")
print(df.head())
Pandas can read many different data sources. You can explore all available options in the official documentation.
Reading Remote Files
If you want, you can also load files directly from the web. Just pass the URL instead of a file path.
1234import pandas as pd df = pd.read_csv("https://staging-content-media-cdn.codefinity.com/courses/64641555-cae4-4cd0-8d29-807aeb6bc0c4/datasets/passengers.csv") print(df.head())
Adjusting Parameters
Even in simple formats like CSV, there is room for different settings. You may want to add or remove a header, replace a comma with another separator, or handle unusual encodings. All this can be configured when loading the file:
import pandas as pd
# Change separator into a semicolon
df1 = pd.read_csv("data.csv", sep=";")
# Specify encoding type
df2 = pd.read_csv("data.csv", encoding="latin1")
# Read file without headers
df3 = pd.read_csv("data.csv", header=None)
# Assign custom column names
df4 = pd.read_csv("data.csv", names=["Date", "Product", "Price"])
1. Which pandas function loads data from a CSV file?
2. How do you read a tab-separated file named data.tsv? (Tab character is \t)
Obrigado pelo seu feedback!