# Load the dataset
df1 = pd.read_csv("matches_clean.csv")
# View the result
print(df1)
ผลลัพธ์:
MatchID HomeTeam AwayTeam HomeGoals AwayGoals MatchDate
0 M001 Manchester United Chelsea 2 1 2024-08-14
1 M002 Liverpool Arsenal 1 1 2024-08-20
2 M003 Tottenham Everton 3 0 2024-09-02
3 M004 Man City Aston Villa 4 2 2024-09-15
4 M005 Newcastle West Ham 0 0 2024-09-22
5 M006 Brighton Leeds 2 3 2024-09-29
# Load the dataset with ";" as delim
df2 = pd.read_csv("matches_semicolon.csv", sep=";")
# View the result
print(df2)
ผลลัพธ์:
MatchID HomeTeam AwayTeam HomeGoals AwayGoals MatchDate
0 M001 Manchester United Chelsea 2 1 2024-08-14
1 M002 Liverpool Arsenal 1 1 2024-08-20
2 M003 Tottenham Everton 3 0 2024-09-02
3 M004 Man City Aston Villa 4 2 2024-09-15
4 M005 Newcastle West Ham 0 0 2024-09-22
5 M006 Brighton Leeds 2 3 2024-09-29
# UK Football Matches Data
# Created for practice with pd.read_csv()
MatchID,HomeTeam,AwayTeam,HomeGoals,AwayGoals,MatchDate
M001,Manchester United,Chelsea,2,1,2024-08-14
M002,Liverpool,Arsenal,1,1,2024-08-20
M003,Tottenham,Everton,3,0,2024-09-02
M004,Man City,Aston Villa,4,2,2024-09-15
M005,Newcastle,West Ham,0,0,2024-09-22
M006,Brighton,Leeds,2,3,2024-09-29
เราสามารถใช้ header ได้แบบนี้:
# Load the dataset where the header is the 3rd row
df3 = pd.read_csv("matches_with_metadata.txt", header=2)
# View the result
print(df3)
ผลลัพธ์:
MatchID HomeTeam AwayTeam HomeGoals AwayGoals MatchDate
0 M001 Manchester United Chelsea 2 1 2024-08-14
1 M002 Liverpool Arsenal 1 1 2024-08-20
2 M003 Tottenham Everton 3 0 2024-09-02
3 M004 Man City Aston Villa 4 2 2024-09-15
4 M005 Newcastle West Ham 0 0 2024-09-22
5 M006 Brighton Leeds 2 3 2024-09-29
# UK Football Matches Data
# Created for practice with pd.read_csv()
MatchID,HomeTeam,AwayTeam,HomeGoals,AwayGoals,MatchDate
M001,Manchester United,Chelsea,2,1,2024-08-14
M002,Liverpool,Arsenal,1,1,2024-08-20
M003,Tottenham,Everton,3,0,2024-09-02
M004,Man City,Aston Villa,4,2,2024-09-15
M005,Newcastle,West Ham,0,0,2024-09-22
M006,Brighton,Leeds,2,3,2024-09-29
เราสามารถใช้ skiprows ได้แบบนี้:
# Load the dataset, skipping the metadata
df4 = pd.read_csv("matches_with_metadata.txt", skiprows=[0, 1])
# View the result
print(df4)
ผลลัพธ์:
MatchID HomeTeam AwayTeam HomeGoals AwayGoals MatchDate
0 M001 Manchester United Chelsea 2 1 2024-08-14
1 M002 Liverpool Arsenal 1 1 2024-08-20
2 M003 Tottenham Everton 3 0 2024-09-02
3 M004 Man City Aston Villa 4 2 2024-09-15
4 M005 Newcastle West Ham 0 0 2024-09-22
5 M006 Brighton Leeds 2 3 2024-09-29
# Load the first 3 rows
df5 = pd.read_csv("matches_clean.csv", nrows=3)
# View the result
print(df5)
ผลลัพธ์:
MatchID HomeTeam AwayTeam HomeGoals AwayGoals MatchDate
0 M001 Manchester United Chelsea 2 1 2024-08-14
1 M002 Liverpool Arsenal 1 1 2024-08-20
2 M003 Tottenham Everton 3 0 2024-09-02
# Load the dataset with MatchID as index col
df7 = pd.read_csv("matches_clean.csv", index_col="MatchID")
# View the result
print(df7)
ผลลัพธ์:
HomeTeam AwayTeam HomeGoals AwayGoals MatchDate
MatchID
M001 Manchester United Chelsea 2 1 2024-08-14
M002 Liverpool Arsenal 1 1 2024-08-20
M003 Tottenham Everton 3 0 2024-09-02
M004 Man City Aston Villa 4 2 2024-09-15
M005 Newcastle West Ham 0 0 2024-09-22
M006 Brighton Leeds 2 3 2024-09-29
# Set col names
col_names = [
"id",
"home",
"away",
"home_goals",
"away_goals",
"date"
]
# Load the dataset with custom col names
df8 = pd.read_csv("matches_no_header.csv", names=col_names)
# View the result
print(df8)
ผลลัพธ์:
id home away home_goals away_goals date
0 M001 Manchester United Chelsea 2 1 2024-08-14
1 M002 Liverpool Arsenal 1 1 2024-08-20
2 M003 Tottenham Everton 3 0 2024-09-02
3 M004 Man City Aston Villa 4 2 2024-09-15
4 M005 Newcastle West Ham 0 0 2024-09-22
5 M006 Brighton Leeds 2 3 2024-09-29
# Set col data types
col_dtypes = {
"MatchID": str,
"HomeGoals": "int32",
"AwayGoals": "int32"
}
# Load the dataset, specifying data types for MatchID, HomeGoals, and AwayGoals
df9 = pd.read_csv("matches_clean.csv", dtype=col_dtypes)
# View the result
df9.info()
# Create a file
with open("example.txt", "x") as file:
file.write("This is the first line.")
file.write("This is the second line.")
file.write("This is the third line.")