How to read CSV File into Python using Pandas | by Barney H.

文章推薦指數: 80 %
投票人數:10人

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



請為這篇文章評分?