將 CSV 文件導入 Pandas 的不同方法

| | | | | | |

讓我們看看如何將 CSV 文件導入 Pandas。

方法 # 1:使用 read_csv() 方法。


# pandas 模塊導入

import pandas as pd


# 創建數據框

df = pd.read_csv ( "< a href="https://media.python.engineering/wp-content/uploads/nba.csv" rel="nofollow noopener noreferrer" target="_blank"> https://media.python.engineering/wp-content /uploads /nba.csv " )

df.head ( 10 )

輸出:

Provide file_path.


# import pandas as pd

import pandas as pd


# 接受文件夾

filepath = r "C: Gfgdatasets ba.csv"


# 讀取 CSV 文件

df = pd.read_csv (filepath)


# 打印前五行

print (df.head())

輸出:

方法二:使用csv 模塊。

可以使用模塊直接導入csv 文件>csv .


# 將模塊導入 csv

import csv

import pandas as pd


# 打開 csv 文件

with open (r " C: UsersAdminDownloads ba.csv " ) as csv_file:


#讀取csv文件

csv_reader = csv.reader (csv_file, delimiter = ` , ` )

# 我們現在可以在 pandas 中使用這些 CSV 文件

df = pd.DataFrame ([csv_reader], index = None )

df.head ()


# 重複第一列的值,Äã,Äã

for val in list (df [ 1 代碼> ]):

打印 (val)

輸出:
< img src = "http://espressocode.top/images/itgranrelscatercden638212.jpg" />

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

12 answers

NUMPYNUMPY

How to convert Nonetype to int or string?

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

12 answers


Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method