pandas.read_csv — pandas 1.5.0 documentation
文章推薦指數: 80 %
Read a comma-separated values (csv) file into DataFrame. Also supports optionally iterating or breaking of the file into chunks.
Ctrl+K
SiteNavigation
Gettingstarted
UserGuide
APIreference
Development
Releasenotes
1.5.0
GitHub
Twitter
SiteNavigation
Gettingstarted
UserGuide
APIreference
Development
Releasenotes
1.5.0
GitHub
Twitter
Input/output
pandas.read_pickle
pandas.DataFrame.to_pickle
pandas.read_table
pandas.read_csv
pandas.DataFrame.to_csv
pandas.read_fwf
pandas.read_clipboard
pandas.DataFrame.to_clipboard
pandas.read_excel
pandas.DataFrame.to_excel
pandas.ExcelFile.parse
pandas.io.formats.style.Styler.to_excel
pandas.ExcelWriter
pandas.read_json
pandas.json_normalize
pandas.DataFrame.to_json
pandas.io.json.build_table_schema
pandas.read_html
pandas.DataFrame.to_html
pandas.io.formats.style.Styler.to_html
pandas.read_xml
pandas.DataFrame.to_xml
pandas.DataFrame.to_latex
pandas.io.formats.style.Styler.to_latex
pandas.read_hdf
pandas.HDFStore.put
pandas.HDFStore.append
pandas.HDFStore.get
pandas.HDFStore.select
pandas.HDFStore.info
pandas.HDFStore.keys
pandas.HDFStore.groups
pandas.HDFStore.walk
pandas.read_feather
pandas.DataFrame.to_feather
pandas.read_parquet
pandas.DataFrame.to_parquet
pandas.read_orc
pandas.DataFrame.to_orc
pandas.read_sas
pandas.read_spss
pandas.read_sql_table
pandas.read_sql_query
pandas.read_sql
pandas.DataFrame.to_sql
pandas.read_gbq
pandas.read_stata
pandas.DataFrame.to_stata
pandas.io.stata.StataReader.data_label
pandas.io.stata.StataReader.value_labels
pandas.io.stata.StataReader.variable_labels
pandas.io.stata.StataWriter.write_file
Generalfunctions
Series
DataFrame
pandasarrays,scalars,anddatatypes
Indexobjects
Dateoffsets
Window
GroupBy
Resampling
Style
Plotting
Optionsandsettings
Extensions
Testing
pandas.read_csv#
pandas.read_csv(filepath_or_buffer,sep=_NoDefault.no_default,delimiter=None,header='infer',names=_NoDefault.no_default,index_col=None,usecols=None,squeeze=None,prefix=_NoDefault.no_default,mangle_dupe_cols=True,dtype=None,engine=None,converters=None,true_values=None,false_values=None,skipinitialspace=False,skiprows=None,skipfooter=0,nrows=None,na_values=None,keep_default_na=True,na_filter=True,verbose=False,skip_blank_lines=True,parse_dates=None,infer_datetime_format=False,keep_date_col=False,date_parser=None,dayfirst=False,cache_dates=True,iterator=False,chunksize=None,compression='infer',thousands=None,decimal='.',lineterminator=None,quotechar='"',quoting=0,doublequote=True,escapechar=None,comment=None,encoding=None,encoding_errors='strict',dialect=None,error_bad_lines=None,warn_bad_lines=None,on_bad_lines=None,delim_whitespace=False,low_memory=True,memory_map=False,float_precision=None,storage_options=None)[source]#
Readacomma-separatedvalues(csv)fileintoDataFrame.
Alsosupportsoptionallyiteratingorbreakingofthefile
intochunks.
Additionalhelpcanbefoundintheonlinedocsfor
IOTools.
Parameters
filepath_or_bufferstr,pathobjectorfile-likeobjectAnyvalidstringpathisacceptable.ThestringcouldbeaURL.Valid
URLschemesincludehttp,ftp,s3,gs,andfile.ForfileURLs,ahostis
expected.Alocalfilecouldbe:file://localhost/path/to/table.csv.
Ifyouwanttopassinapathobject,pandasacceptsanyos.PathLike.
Byfile-likeobject,werefertoobjectswitharead()method,suchas
afilehandle(e.g.viabuiltinopenfunction)orStringIO.
sepstr,default‘,’Delimitertouse.IfsepisNone,theCenginecannotautomaticallydetect
theseparator,butthePythonparsingenginecan,meaningthelatterwill
beusedandautomaticallydetecttheseparatorbyPython’sbuiltinsniffer
tool,csv.Sniffer.Inaddition,separatorslongerthan1characterand
differentfrom'\s+'willbeinterpretedasregularexpressionsand
willalsoforcetheuseofthePythonparsingengine.Notethatregex
delimitersarepronetoignoringquoteddata.Regexexample:'\r\t'.
delimiterstr,defaultNoneAliasforsep.
headerint,listofint,None,default‘infer’Rownumber(s)touseasthecolumnnames,andthestartofthe
data.Defaultbehavioristoinferthecolumnnames:ifnonames
arepassedthebehaviorisidenticaltoheader=0andcolumn
namesareinferredfromthefirstlineofthefile,ifcolumn
namesarepassedexplicitlythenthebehaviorisidenticalto
header=None.Explicitlypassheader=0tobeableto
replaceexistingnames.Theheadercanbealistofintegersthat
specifyrowlocationsforamulti-indexonthecolumns
e.g.[0,1,3].Interveningrowsthatarenotspecifiedwillbe
skipped(e.g.2inthisexampleisskipped).Notethatthis
parameterignorescommentedlinesandemptylinesif
skip_blank_lines=True,soheader=0denotesthefirstlineof
dataratherthanthefirstlineofthefile.
namesarray-like,optionalListofcolumnnamestouse.Ifthefilecontainsaheaderrow,
thenyoushouldexplicitlypassheader=0tooverridethecolumnnames.
Duplicatesinthislistarenotallowed.
index_colint,str,sequenceofint/str,orFalse,optional,defaultNoneColumn(s)touseastherowlabelsoftheDataFrame,eithergivenas
stringnameorcolumnindex.Ifasequenceofint/strisgiven,a
MultiIndexisused.
Note:index_col=Falsecanbeusedtoforcepandastonotusethefirst
columnastheindex,e.g.whenyouhaveamalformedfilewithdelimitersat
theendofeachline.
usecolslist-likeorcallable,optionalReturnasubsetofthecolumns.Iflist-like,allelementsmusteither
bepositional(i.e.integerindicesintothedocumentcolumns)orstrings
thatcorrespondtocolumnnamesprovidedeitherbytheuserinnamesor
inferredfromthedocumentheaderrow(s).Ifnamesaregiven,thedocument
headerrow(s)arenottakenintoaccount.Forexample,avalidlist-like
usecolsparameterwouldbe[0,1,2]or['foo','bar','baz'].
Elementorderisignored,sousecols=[0,1]isthesameas[1,0].
ToinstantiateaDataFramefromdatawithelementorderpreserveduse
pd.read_csv(data,usecols=['foo','bar'])[['foo','bar']]forcolumns
in['foo','bar']orderor
pd.read_csv(data,usecols=['foo','bar'])[['bar','foo']]
for['bar','foo']order.
Ifcallable,thecallablefunctionwillbeevaluatedagainstthecolumn
names,returningnameswherethecallablefunctionevaluatestoTrue.An
exampleofavalidcallableargumentwouldbelambdax:x.upper()in
['AAA','BBB','DDD'].Usingthisparameterresultsinmuchfaster
parsingtimeandlowermemoryusage.
squeezebool,defaultFalseIftheparseddataonlycontainsonecolumnthenreturnaSeries.
Deprecatedsinceversion1.4.0:Append.squeeze("columns")tothecalltoread_csvtosqueeze
thedata.
prefixstr,optionalPrefixtoaddtocolumnnumberswhennoheader,e.g.‘X’forX0,X1,…
Deprecatedsinceversion1.4.0:UsealistcomprehensionontheDataFrame’scolumnsaftercallingread_csv.
mangle_dupe_colsbool,defaultTrueDuplicatecolumnswillbespecifiedas‘X’,‘X.1’,…’X.N’,ratherthan
‘X’…’X’.PassinginFalsewillcausedatatobeoverwrittenifthere
areduplicatenamesinthecolumns.
Deprecatedsinceversion1.5.0:Notimplemented,andanewargumenttospecifythepatternforthe
namesofduplicatedcolumnswillbeaddedinstead
dtypeTypenameordictofcolumn->type,optionalDatatypefordataorcolumns.E.g.{‘a’:np.float64,‘b’:np.int32,
‘c’:‘Int64’}
Usestrorobjecttogetherwithsuitablena_valuessettings
topreserveandnotinterpretdtype.
Ifconvertersarespecified,theywillbeappliedINSTEAD
ofdtypeconversion.
Newinversion1.5.0:Supportfordefaultdictwasadded.Specifyadefaultdictasinputwhere
thedefaultdeterminesthedtypeofthecolumnswhicharenotexplicitly
listed.
engine{‘c’,‘python’,‘pyarrow’},optionalParserenginetouse.TheCandpyarrowenginesarefaster,whilethepythonengine
iscurrentlymorefeature-complete.Multithreadingiscurrentlyonlysupportedby
thepyarrowengine.
Newinversion1.4.0:The“pyarrow”enginewasaddedasanexperimentalengine,andsomefeatures
areunsupported,ormaynotworkcorrectly,withthisengine.
convertersdict,optionalDictoffunctionsforconvertingvaluesincertaincolumns.Keyscaneither
beintegersorcolumnlabels.
true_valueslist,optionalValuestoconsiderasTrue.
false_valueslist,optionalValuestoconsiderasFalse.
skipinitialspacebool,defaultFalseSkipspacesafterdelimiter.
skiprowslist-like,intorcallable,optionalLinenumberstoskip(0-indexed)ornumberoflinestoskip(int)
atthestartofthefile.
Ifcallable,thecallablefunctionwillbeevaluatedagainsttherow
indices,returningTrueiftherowshouldbeskippedandFalseotherwise.
Anexampleofavalidcallableargumentwouldbelambdax:xin[0,2].
skipfooterint,default0Numberoflinesatbottomoffiletoskip(Unsupportedwithengine=’c’).
nrowsint,optionalNumberofrowsoffiletoread.Usefulforreadingpiecesoflargefiles.
na_valuesscalar,str,list-like,ordict,optionalAdditionalstringstorecognizeasNA/NaN.Ifdictpassed,specific
per-columnNAvalues.Bydefaultthefollowingvaluesareinterpretedas
NaN:‘’,‘#N/A’,‘#N/AN/A’,‘#NA’,‘-1.#IND’,‘-1.#QNAN’,‘-NaN’,‘-nan’,
‘1.#IND’,‘1.#QNAN’,‘
延伸文章資訊
- 1Creating a dataframe using CSV files - GeeksforGeeks
CSV files are the “comma-separated values”, these values are separated by commas, this file can b...
- 2How 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...
- 3How to read CSV File into Python using Pandas | by Barney H.
- 4pandas.read_csv — pandas 1.5.0 documentation
Read a comma-separated values (csv) file into DataFrame. Also supports optionally iterating or br...
- 5[Day07]Pandas操作資料的函數! - iT 邦幫忙
使用Python進行資料分析系列第7 篇 ... 第七天了,上一篇提介紹了python內的一個套件pandas內兩個重要的資料結構, ... 使用 read_csv() 讀取一個CSV的檔案