How to read CSV File into Python using Pandas | by Barney H.
文章推薦指數: 80 %
Pandas read_csv() function imports a CSV file to DataFrame format. Here are some options: filepath_or_buffer: this is the file name or file path OpeninappHomeNotificationsListsStoriesWritePublishedinTowardsDataScienceHowtoreadCSVFileintoPythonusingPandasInthispost,we’llgooverhowtoimportaCSVFileintoPython.PhotobyAbsolutVisiononUnsplashShortAnswerTheeasiestwaytodothis:importpandasaspddf=pd.read_csv('file_name.csv')print(df)Ifyouwanttoimportasubsetofcolumns,simplyaddusecols=['column_name'];pd.read_csv('file_name.csv',usecols=['column_name1','column_name2'])Ifyouwanttouseanotherseparator,simplyaddsep='\t';Defaultseparatoris','.pd.read_csv('file_name.csv',sep='\t')RecaponPandasDataFramePandasDataFramesisanexcellikedatastructurewithlabeledaxes(rowsandcolumns).HereisanexampleofpandasDataFramethatwewilluseasanexamplebelow:CodetogenerateDataFrame:ImportingaCSVfileintotheDataFramePandasread_csv()functionimportsaCSVfiletoDataFrameformat.Herearesomeoptions:filepath_or_buffer:thisisthefilenameorfilepathdf.read_csv('file_name.csv’)#relativepositiondf.read_csv('C:/Users/abc/Desktop/file_name.csv')header:thisallowsyoutospecifywhichrowwillbeusedascolumnnamesforyourdataframe.Expectedanintvalueoralistofintvalues.Defaultvalueisheader=0,whichmeansthefirstrowoftheCSVfilewillbetreatedascolumnnames.Ifyourfiledoesn’thaveaheader,simplysetheader=None.df.read_csv('file_name.csv’,header=None)#noheaderTheoutputofnoheader:sep:SpecifyacustomdelimiterfortheCSVinput,thedefaultisacomma.pd.read_csv('file_name.csv',sep='\t')#UseTabtoseparateindex_col:Thisistoallowyoutosetwhichcolumnstobeusedastheindexofthedataframe.ThedefaultvalueisNone,andpandaswilladdanewcolumnstartfrom0tospecifytheindexcolumn.Itcanbesetasacolumnnameorcolumnindex,whichwillbeusedastheindexcolumn.pd.read_csv('file_name.csv',index_col='Name')#Use'Name'columnasindexnrows:Onlyreadthenumberoffirstrowsfromthefile.Needsanintvalue.usecols:Specifywhichcolumnstoimporttothedataframe.Itcanalistofintvaluesorcolumnnames.pd.read_csv('file_name.csv',usecols=[1,2,3])#Onlyreadscol1,col2,col3.col0willbeignored.pd.read_csv('file_name.csv',usecols=['Name'])#Onlyreads'Name'column.Othercolumnswillbeignored.converters:Helpstoconvertvaluesinthecolumnsbydefinedfunctions.na_values:ThedefaultmissingvalueswillbeNaN.UsethisifyouwantotherstringstobeconsideredasNaN.Theexpectedinputisalistofstrings.pd.read_csv('file_name.csv',na_values=['a','b'])#aandbvalueswillbetreatedasNaNafterimportingintodataframe.MorefromTowardsDataScienceFollowYourhomefordatascience.AMediumpublicationsharingconcepts,ideasandcodes.ReadmorefromTowardsDataScienceRecommendedfromMediumStefanWolpersinFoodforAgileThoughtFoodforAgileThought#154ToyotaVenturesinToyotaVenturesProvidingtheEssentialBuildingBlocksforRobotics:OurPortfolioCompanyFreedomRoboticsIs…AbbeyAzizIntroductionMayaBasicGUIinPythonKolteninStellarCommunityStellarDevDigest:Issue#48LionelMartininHackerNoon.comHowtointegrateHubspotwithanything—andbuildyourowntoolsNiravdholiyaFlutterandDesktopApps:ProgresstowardsanambientcomputingvisionMotoBabboGettingStartedwithPython: howpythonworksMakersinMakersWanttogetinspiredbysomeofthesharpestnewtalentintech?AboutHelpTermsPrivacyGettheMediumappGetstartedBarneyH.309FollowersPythonenthusiast|SoftwareEngineer@Google|IloveAPI|FollowHelpStatusWritersBlogCareersPrivacyTermsAboutKnowable
延伸文章資訊
- 1[Day07]Pandas操作資料的函數! - iT 邦幫忙
使用Python進行資料分析系列第7 篇 ... 第七天了,上一篇提介紹了python內的一個套件pandas內兩個重要的資料結構, ... 使用 read_csv() 讀取一個CSV的檔案
- 2在python利用Pandas 讀、寫csv檔及轉成list,對資料進行增減 ...
import pandas as pd. 首先引入pandas car_ = pd.read_csv('car.csv'). car.csv 為欲讀取之檔案,car_ 為DataFrame 格式...
- 3How to Read CSV Files with NumPy? - GeeksforGeeks
- 4How to Import a CSV File into Python using Pandas
Step 1: Capture the File Path. Firstly, capture the full path where your CSV file is stored. · St...
- 5Python | Read csv using pandas.read_csv() - GeeksforGeeks
To access data from the CSV file, we require a function read_csv() that retrieves data in the for...