Basic information about a data frame#

using DataFrames

Let’s start by creating a DataFrame object, x, so that we can learn how to get information on that data frame.

x = DataFrame(A=[1, 2], B=[1.0, missing], C=["a", "b"])
2×3 DataFrame
RowABC
Int64Float64?String
111.0a
22missingb

The standard size function works to get dimensions of the DataFrame,

size(x), size(x, 1), size(x, 2)
((2, 3), 2, 3)

as well as nrow and ncol from R.

nrow(x), ncol(x)
(2, 3)

describe gives basic summary statistics of data in your DataFrame (check out the help of describe for information on how to customize shown statistics).

describe(x)
3×7 DataFrame
Rowvariablemeanminmedianmaxnmissingeltype
SymbolUnion…AnyUnion…AnyInt64Type
1A1.511.520Int64
2B1.01.01.01.01Union{Missing, Float64}
3Cab0String

you can limit the columns shown by describe using cols keyword argument

describe(x, cols=1:2)
2×7 DataFrame
Rowvariablemeanminmedianmaxnmissingeltype
SymbolFloat64RealFloat64RealInt64Type
1A1.511.520Int64
2B1.01.01.01.01Union{Missing, Float64}

names will return the names of all columns as strings

names(x)
3-element Vector{String}:
 "A"
 "B"
 "C"

you can also get column names with a given element type (eltype):

names(x, String)
1-element Vector{String}:
 "C"

use propertynames to get a vector of Symbols:

propertynames(x)
3-element Vector{Symbol}:
 :A
 :B
 :C

eltype on eachcol(x) returns element types of columns:

eltype.(eachcol(x))
3-element Vector{Type}:
 Int64
 Union{Missing, Float64}
 String

Here we create some large DataFrame

y = DataFrame(rand(1:10, 1000, 10), :auto)
1000×10 DataFrame
975 rows omitted
Rowx1x2x3x4x5x6x7x8x9x10
Int64Int64Int64Int64Int64Int64Int64Int64Int64Int64
156812557105
287810499557
39357976854
4697101361065
536116485310
65943925239
7105163158710
8134916101076
972696107915
1016161088344
11258921075910
127225725771
13762177310103
9898185773194
990105459239104
9913362136836
99212286139710
9939664668968
994910104185536
9956381012101081
99691036288716
997986451102104
998107113881107
9997782164247
100016105117333

and then we can use first to peek into its first few rows

first(y, 5)
5×10 DataFrame
Rowx1x2x3x4x5x6x7x8x9x10
Int64Int64Int64Int64Int64Int64Int64Int64Int64Int64
156812557105
287810499557
39357976854
4697101361065
536116485310

and last to see its bottom rows.

last(y, 3)
3×10 DataFrame
Rowx1x2x3x4x5x6x7x8x9x10
Int64Int64Int64Int64Int64Int64Int64Int64Int64Int64
1107113881107
27782164247
316105117333

Using first and last without number of rows will return a first/last DataFrameRow in the DataFrame

first(y)
DataFrameRow (10 columns)
Rowx1x2x3x4x5x6x7x8x9x10
Int64Int64Int64Int64Int64Int64Int64Int64Int64Int64
156812557105
last(y)
DataFrameRow (10 columns)
Rowx1x2x3x4x5x6x7x8x9x10
Int64Int64Int64Int64Int64Int64Int64Int64Int64Int64
100016105117333

Displaying large data frames#

Create a wide and tall data frame:

df = DataFrame(rand(100, 100), :auto)
100×100 DataFrame
75 rows omitted
Rowx1x2x3x4x5x6x7x8x9x10x11x12x13x14x15x16x17x18x19x20x21x22x23x24x25x26x27x28x29x30x31x32x33x34x35x36x37x38x39x40x41x42x43x44x45x46x47x48x49x50x51x52x53x54x55x56x57x58x59x60x61x62x63x64x65x66x67x68x69x70x71x72x73x74x75x76x77x78x79x80x81x82x83x84x85x86x87x88x89x90x91x92x93x94x95x96x97x98x99x100
Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64
10.8442560.06688230.5670.5516570.1184840.338530.3108550.9451710.385070.8964640.4334520.6810740.9049670.004714350.6826580.6040320.4166150.7763680.7569320.05581090.3411860.3111180.5923040.3413590.8943940.1406790.8731050.477310.9416540.5154040.7686890.5797430.7966810.8067070.1316180.880320.2495260.8166850.3175920.6247530.1902060.9021420.421820.4720720.2798610.6261060.9653810.07139340.0712380.08443410.3688820.3986650.9844550.9903620.3640610.7346490.710060.08162890.1518060.3122850.4892280.2486250.3378030.02840220.1016030.8701320.00798020.6168750.3731250.766870.6056870.5074840.4553440.5605650.6155690.2344430.03458260.9233560.9313150.6582230.1016960.9449350.8993390.4902520.2234670.3503670.2583720.8073890.2510740.3585880.2327260.6545990.7091130.1421540.3571040.8840080.2198390.6109660.6919030.407382
20.87190.4686180.9571910.07964040.7243660.6082160.4685990.9968930.4419090.4089790.9658910.7463030.1653130.885330.2003380.1495250.009511780.01308560.5198660.6833690.2827950.2309320.8138910.8840860.5835860.2350990.4344280.7396890.5800610.4817870.58430.3411570.816730.6170810.531740.6549570.5895370.3144710.3393530.3760140.1801120.06396370.4826240.8395490.6210740.6170050.9900050.4566620.3963910.7879580.6388210.2428730.06437080.201360.7887860.4651790.5207640.1698160.2969660.4857670.06730180.9222430.965410.650010.06601990.3132050.5691320.1324340.194630.1494810.6791690.6535490.1022550.6363190.774140.4970320.8106010.3844720.1278710.4595840.01778150.8405440.5623440.4984240.0671330.387330.7496780.4862140.818360.8592340.2330770.5443170.312770.1829590.0453550.5387290.5869770.04831140.4429110.877546
30.5776790.544050.6618180.8984520.5861210.1209880.02541610.3487590.7014730.4788150.547390.8029380.6914430.6052060.8232980.6668950.7106620.357560.1447280.32790.4065830.1821820.8805480.7500870.4615790.1350320.1979660.8779990.5834120.3253060.2979850.967210.8875240.9195770.02594270.2779060.8662090.7312840.4352290.09795850.9479370.390690.5117590.878920.2767920.3491660.07265040.1298840.5794660.1567580.6576550.8489340.3608110.0452570.05120560.897910.668760.6558650.8413880.08482270.6986580.4813410.786820.5408740.3562720.09091410.1795150.8797150.5481360.0102930.02452660.3796050.4301490.1965890.2284280.5633460.7749230.4265560.9999860.03680510.5696020.5310570.9227930.5025680.4014440.4007280.9266690.9626790.5606570.05325960.682140.0908030.9727790.4653250.7336170.6706850.8467460.6361780.9107080.262608
40.1167270.4327820.6225280.2955610.5681110.6682790.4610440.5154110.403980.6461360.8774880.8946520.1484780.3621090.3241420.4214360.2915060.3586360.3276350.1914080.9878140.2634710.8984790.426290.5978180.3662350.7731580.9276210.6851190.9166760.4051910.2295310.1611310.9178580.6521190.4466820.9847990.6503270.6456530.6839860.5083470.612940.02672610.6069890.5048750.5154020.1904550.5853640.2964020.3194730.02649140.5832440.1097460.05860450.8080670.824810.2094330.04091150.3077890.1318180.9824620.7494490.6708810.9984480.3839910.596020.6952910.07660790.5695840.3710740.3828020.8501120.4594440.726510.08139820.1821590.4157620.2229440.5112680.1645450.7891410.496340.9141930.775120.5006970.6552480.5395980.9707640.008266680.1195410.9384810.8573010.149260.8293860.8577350.1456120.09569170.3085340.3490920.315909
50.6836710.6130490.2202980.4382280.02426660.8429350.006993450.09017590.3939770.8377570.3248780.9932850.8457410.1923770.8997860.734850.8709010.9106140.5512060.2056340.6808490.01183820.3677510.3356580.01265080.7562970.09644260.2324370.8796980.2000290.3199040.1561920.2317530.02301830.3409590.344180.5847210.001662820.05971070.8530110.7244910.777230.1012410.1149140.7279230.5004030.2013610.4792420.4901650.3549880.3961910.07094120.882490.4756630.8798430.01290580.2455220.3025280.4442660.9290240.09315250.3209360.2546380.390820.3255670.09506850.5453840.6329490.6600340.7102680.2114630.351060.3495510.7265510.5473990.6846580.6095380.8332620.5032730.1489340.7816480.2422760.4964790.4327460.3335360.2323930.007335590.3515150.3930750.8870830.6668910.649410.901670.6226130.07587210.6150660.1234560.3762460.7773390.569178
60.4278340.5139490.566780.9994360.4238710.8372480.9102950.8936710.7779880.7135840.4552650.2474840.1768130.4706710.004854960.3196080.4495280.2167570.3888580.8267970.6565390.2686440.02047470.04914770.9808350.2682220.819930.8694260.4479270.9227710.5666180.5875390.01845380.3497330.3924650.0008158820.24390.2326840.01696870.3825670.1767830.4483830.09883240.4410.5295860.5943640.5307590.1515660.9597760.9837950.99990.7302240.9150130.8485470.3339230.1708530.8797590.3212090.5302870.1373370.08010940.2488640.4401590.8801930.8953810.1310220.758710.2453070.7955520.1788820.5859380.7585090.2094370.9440040.5587730.1934380.06641820.2617070.1505080.559760.6405440.2156340.9148950.06173840.7409180.3605970.7887360.6010130.7272970.2630430.4220490.5959740.7039110.06048310.6442510.875110.9219290.1877560.3476670.148685
70.9095570.3259110.4585640.3186630.3909090.2186760.6040940.04478510.6404040.1463710.6404990.06559880.8055890.8312740.03024910.9451410.135960.6715620.8660380.3187040.360810.8631790.06236190.975270.6693020.2478930.298380.96870.03203240.6038450.7252420.1412120.9181360.8750590.0391370.3440680.8427040.46150.4138030.7248920.7223020.124660.6776760.9804350.2563980.8190510.5741020.783040.541630.493430.3421150.1019330.6427270.5662230.9590890.01102820.1006820.2454960.9802270.6637610.8482190.03698580.3098970.3305810.8001410.2945770.3517270.8233520.6560060.896470.1114450.684290.5595550.6715260.5279070.2537070.2862680.3776890.3731790.2434780.9024550.3020390.08075670.1094120.7332640.1494260.7983920.6525090.8032980.189180.09883490.8416150.7275970.2271180.8315310.2841120.5645420.4594860.09743770.675666
80.4247050.8270750.2937870.4712370.6038410.3904150.4913160.1887390.7893930.7738390.005332270.1380030.6079410.592050.2271280.5600620.8708750.9281860.7020860.2288910.6892150.1523150.851110.36230.7495710.2006670.4552540.0274580.6755570.04243530.7190210.9187710.9195110.05337870.3237910.5956760.5259470.1042780.2189730.8173950.8176430.2225740.7380820.4211610.965390.0298530.8917060.9941360.1941420.05983730.7338370.2572620.697430.7989030.5912610.09541830.1601950.8487460.04782820.7290930.6859540.1376340.182940.3215190.1767080.7368770.06078250.6860660.3722310.3147810.05242440.4450820.05951430.02679790.3432890.9127390.6140650.9129660.1698790.8541430.9638810.6418740.2319830.9750160.7083570.7060580.8336060.5163350.5007750.7387280.6537760.9425420.03190850.6057540.3284120.4139870.7994220.1810610.9310640.848057
90.5491520.7276590.9812840.09466320.7552680.6060060.1208960.6712930.7428030.4199510.3225310.4574280.9767830.03834650.2431980.216850.06982210.5917960.9964980.1680070.4727070.3122320.4628520.5368640.9341750.03786860.7234240.1540360.4792720.02719980.7646830.709260.1912570.161880.7571150.7916280.03385890.1823940.4610.9020130.4757750.5329960.9880180.2450850.6290590.3641870.01323980.8316640.266770.5589110.8653460.7568080.9392710.3559440.3159210.2643440.6677360.4435340.5188810.2035470.7629730.4247110.8991590.03072840.586410.1785620.3563140.9060660.5020820.700610.8613810.4604470.7456170.004930360.6369030.136080.6544460.9583810.6433640.5794620.2656920.2441020.8021690.2390090.7018750.3515770.9946840.03299270.5701440.23540.6668620.2742540.6729140.7171520.3710340.1735430.5478320.3389920.3655340.59494
100.7074280.9969080.1127660.7107250.6487950.6373750.2983560.2196960.04663710.846390.0916380.1945470.7319710.7656140.4342370.6221450.4629380.6898970.8749730.1609520.5427620.9223890.9648840.06909830.8376540.4048740.5584880.3109010.2675930.4966570.7639220.8835890.9514210.5213440.04552610.5761690.478810.638720.5425860.1682230.7547870.9969330.457870.9311030.1932430.7814950.164830.4887560.8150960.03325040.2848980.9520990.2917420.269050.3906610.5426630.8939740.03213040.6696580.3268280.6342950.3842550.7551670.3044690.1745780.2615320.9617020.6763780.1753430.8468170.2699810.7907540.5896230.9553920.1757060.4491020.3604760.7726370.3512390.8209920.08488910.8279530.245670.9020810.7880830.02022280.3404720.8238250.8596380.7447580.14960.8869490.655030.7500870.635950.5539310.0632110.3158070.5774650.689816
110.6997510.521980.4475710.1484410.9268070.4205470.05015670.1380790.1113720.3002320.8333430.01152850.6775820.3797220.3555360.0816320.9089380.7593320.8034080.4258940.5740830.7068470.4986890.02814390.755520.7382990.01093230.6245710.786450.6578790.1180.5602520.4114750.970060.449860.2219530.5469560.7424790.8546550.2628050.09559640.5254640.05709730.169530.3986550.02510460.8794490.09105190.6823820.9771960.8853420.6078850.03809030.3164890.4440040.08633780.1673920.5446940.2066780.2292910.8499230.9376210.641720.9409340.4105840.6002620.01472630.3301380.7120650.3093740.5796450.4105980.2402850.9236850.5090680.1848820.06107150.901650.5631120.3166740.4064720.1772180.8442840.6145150.5396520.1999760.9960710.2975340.8466770.8810070.3119130.6679610.6389640.5759240.2532870.8609420.4694460.7526790.7346560.3209
120.8847870.2716270.575340.008414840.27320.6210880.8885460.6299690.550220.275740.5634760.8703390.7867860.02126320.6823770.002400550.1580480.4566820.4312550.5683870.4117010.6445080.784620.2297290.05151070.9011330.541510.8779040.1511960.3715790.7701980.6048730.7479230.5509730.8701920.5424420.6235450.4593290.2123160.5174550.8408770.4982990.07853410.5698580.6118560.8097760.683020.8821250.2884320.3997140.7608130.2319340.1093830.7576880.2765370.1042650.9953630.6655230.1876140.720080.4748940.3967620.341960.4750210.9442120.1205040.3822220.1374410.1398790.1282720.830920.2212830.4206980.6588290.4793140.6803710.2586950.01034290.8128890.2021750.6176710.7387910.767980.5590450.01752110.3759870.6648690.04964660.3658740.1567730.1102890.2578660.4648070.3499480.978840.7610720.9348370.07723910.1475970.804986
130.6787880.8847820.2669710.5862820.8093380.6602750.5696870.6917720.1974580.6732890.7492330.9423530.7742460.9366630.7427440.1826290.6397420.3678410.8744620.6072150.9314940.7982890.6104650.473210.04434670.6794240.05477790.1831490.1628770.7511920.7549360.3627770.749090.5644140.3581430.2058370.4703620.3981220.5055120.7596280.7782120.1994350.1400160.7829370.26360.4121730.3894940.8871610.8540730.09443350.2987310.396610.353610.4311930.2522910.4923320.01498220.7573330.2719970.9450650.7555960.07175970.1328740.356890.8922290.6915650.1440090.1664690.412890.6273710.648720.8778770.9884590.7756460.005040940.05490130.9971690.2392440.5607750.9519180.013840.7424690.2536780.1627610.6432640.9665950.1591580.638060.422770.9531540.3589960.1595410.2074360.1966810.8900510.01759690.9646370.3263270.7110320.0082302
890.2128040.2938850.2127180.2530220.6356240.1448870.4223220.3738220.5700560.4191620.7005710.3935720.6158920.1428510.9972690.3053270.1777780.1653510.9099810.9883520.8824590.7733980.2442660.763940.2967290.01821270.5931180.7250670.532270.9345410.957770.8688660.4476680.3075920.008489960.7375780.3777810.2206270.855010.8435210.4877390.9639380.224720.5895780.1828210.9349390.3493690.7010550.04625770.6621150.01348040.2862550.1470010.9122760.7162690.2407750.6362910.3324110.07865170.08002590.02667110.1550380.3787080.432950.5266080.2526960.3190680.257870.2087780.617030.1653490.8501230.9325680.7933520.8592510.7579590.1236090.8057960.3126780.6256760.6175760.09861510.5178650.190220.163840.6624720.904690.5361870.8455040.5365120.1183120.5491360.2485380.4129620.7161180.4267390.9458210.04955020.960290.738079
900.09137940.6545360.09406190.5631860.2114820.6070190.8510360.7553780.1930650.0140650.7760830.2299830.1593310.9404630.3965190.9070330.9891920.1321430.2716480.09765830.4812930.2468120.968560.1316550.3633640.6688540.8706050.421350.07371530.6874250.439950.01611290.1164820.2057120.7410620.0858030.6866930.5949670.1290490.9546390.6051720.7692870.0236130.689180.05899670.2148870.722430.9444570.2644330.9756970.1631740.2979910.7923240.4158920.3221540.7353290.543270.6961510.7982660.8261760.271520.9656440.2291730.7717350.7290960.657930.5128930.3820120.7379520.1989810.5528840.9340680.3485980.8015690.7964820.1835270.05857270.08621740.8240170.667380.5618220.6218150.06575010.9813210.4543460.6512650.7740280.350520.3947440.734990.6787650.06518050.09542620.8993440.3434890.4275860.9587920.7801190.09532890.967183
910.6361170.8947760.4021840.7171160.8359040.9175790.1960510.690530.2853050.5422770.9010480.429460.08408530.2004650.5211540.8813450.9185630.2250360.008860870.16060.7481280.6397040.4862320.1454140.1517050.247950.6192110.1841440.6596610.3563180.4076470.6454060.3509430.9813040.5958870.04121240.5986010.1617930.01818550.7935930.4643510.7885510.09395640.6060110.8722850.8025290.05549740.1907430.8544710.8001870.97980.4448820.2592070.1194310.315730.4110140.6330950.7456160.08041280.3060690.8799850.634390.3425830.6297440.4408680.1497750.7788290.8326820.401660.5260380.7124530.7591130.8532330.07591760.9191180.4046260.5476540.02464370.09577540.9262010.8409670.7814050.04623640.271760.4456320.8421590.9609820.04300690.3367730.628910.6263770.6866360.2767090.4844820.9935450.03728090.2856180.7203830.1777090.798112
920.05732650.6822490.4981010.169990.4738450.1185520.8047810.4917640.4508610.9043770.4989750.1275020.07621880.2532290.4455870.5072420.0498710.8977890.5192750.002621510.9343470.7604360.9996120.870070.28310.4858730.8993240.5714820.7247880.6796370.2649380.8603510.4819390.5396140.3602850.1415920.8280880.5151810.5055360.2555360.5271110.2770380.6421640.3415860.7236110.117530.4629710.7023160.5573230.3464590.1463610.0544160.7038550.1203830.6510830.3527010.4332990.2846310.9642960.5368540.09986560.7227140.1248380.5870840.2373820.7379990.9090640.8867130.1288670.5140560.2338780.2883180.3240390.1664510.4720480.9890760.5396810.3674080.6921030.3481810.04435030.5882260.1642410.3063450.04686590.6623980.6985640.4001830.2711950.6115160.2081440.04009240.903070.4005410.7377630.1955480.05459860.2077260.3570850.104791
930.5615820.335460.8832180.6076580.00630940.62830.3995180.7856640.5726790.8960890.5717640.7711290.8649710.1356880.6623280.1335690.6877290.3261010.2729250.5282010.05257590.8174940.1787970.7398060.4542530.7143750.5661690.03281960.7587380.2670490.845430.3771950.6966320.1285460.9208940.1321860.2021110.05924460.2122550.7614030.9406730.9190460.8273190.06017510.08354550.6132360.3480110.3185040.7149140.02436290.3647260.1313090.7250630.5538060.2023010.9063840.0467470.2489710.3459440.1805430.06049420.6977790.4167310.7394880.2113710.0861430.7739610.844440.2938170.5634130.4496980.5269470.7811770.9276690.4158960.8075360.2013930.01627870.1216160.06926430.5626670.4721710.156880.1461360.9187290.955080.8876440.03487860.05218440.06704010.2722720.7999410.7349830.8062320.7093170.01939360.877940.8108160.1151220.272992
940.4621190.1875930.5208430.9588330.5978220.2203150.2337380.6479950.3657180.906010.252870.4364010.5054230.5832390.7269380.8688670.4778390.4810650.8075720.3424790.1034470.9832760.484310.636250.1195480.1130980.7010970.5197820.1296620.9844590.1841260.8372890.2967190.05151280.6705030.06339740.9910030.8922420.5545540.3422560.8973060.8845880.01290270.106760.2506340.003164710.7321550.7059690.8480670.8250620.3039240.6777940.5842750.4171370.3655280.5726290.7414510.2254930.4859560.380580.03583460.1295320.2454110.5966290.4472110.1237750.3219720.8492720.2966320.3359530.4836540.4600940.7572260.7513840.4951150.2994480.909240.5773030.8851920.5246930.2785940.4240110.2247880.09118120.2857970.1463630.006740870.2331290.4073120.6723580.5967710.01988170.6772150.8211980.294210.9519270.9375240.8105090.3357330.805719
950.7925230.7958410.08095420.7710540.8122730.3117220.8515080.2424420.2153180.8722770.7607580.04763950.101720.7876280.3865450.2303450.1965930.1494240.5857820.9382520.3050260.7021490.1572920.3094270.1593190.2857440.8673210.780070.5611440.9119510.8161950.6984710.344570.2539680.2007750.4470190.3906490.4036720.346210.3337670.7184060.6276220.7343410.590270.6402940.9781880.6937380.3663210.6514780.2027720.9063150.6699010.5958840.6859870.4026640.9394140.0810110.5825610.3113180.2143040.9555470.5838070.3543880.4183960.6041740.9559170.5452550.2053570.4129270.009332960.3252930.9081140.2530890.2946790.3072180.03549210.5474740.4587720.8304490.3318640.8617960.3950690.5214960.4800370.3222740.8570030.6129950.08723490.6081510.9039440.5877430.3939460.1859680.6134420.5901780.067380.8594090.922040.583290.918472
960.7982490.01393520.3899790.8634960.7283450.9678890.6491750.7985390.2518660.9232030.9373030.3082810.9931120.1552330.2600570.9514740.7209150.5355390.261280.749980.8598510.1748970.9320230.9279770.5237590.7867830.5347490.08530610.2613210.2303770.5146210.6439370.07351210.9301310.2082740.08215130.5622420.3804010.5569690.9661290.1625210.1662160.9788080.5373410.5938960.6235870.3559430.04326510.4206250.409160.5739750.2173280.09203980.009652730.9395560.9883860.4786610.675740.6551610.03954860.4995080.4437210.1734950.2018960.1451390.4402450.5288130.9753980.6816230.0495470.7044530.2218730.9427170.8843820.9071290.009552130.7784930.481990.4402670.2617210.01736280.6261730.0402790.6601510.553840.8520730.04572960.8024620.9341020.73830.3090250.462340.01171110.7556790.9172740.505760.2292830.3311370.1254540.760506
970.5196670.9550740.4538710.08619050.2722530.9562060.05942650.8936160.9174920.4967020.5633750.9174440.3664190.6707580.9631130.2835390.01188280.7380110.3788380.5737880.02680450.04661850.9562270.2925440.5357310.1148170.1477850.6582150.9037430.7957690.7009030.009966350.6945720.6113610.8640480.6132980.8715630.2488880.3364570.3768550.6777330.454850.9767420.7378640.9473940.9448910.7188980.2978170.4654720.3837390.8663610.4103080.6022450.0658450.52960.175360.8446350.8408680.9526860.4176460.4408740.4918520.4836130.1624640.1132580.4007730.7674140.830940.7961520.08169130.2014560.1121490.01452830.1813280.6678660.3797250.09097130.01813960.06681450.1007540.04956760.3922980.2161890.0003945950.6845540.6548230.3824310.8516060.03273130.3071850.0848580.3833470.4465310.1907510.06314610.9405050.8922950.1679190.3198830.20966
980.235210.35990.8708820.6617480.5571670.9318690.9755860.5908990.4705680.9893190.8501040.900560.0591270.550020.2584630.1492080.7300020.2343070.4973230.07093070.5399410.2401370.9551930.8622320.9949530.1230360.3843650.9283230.4517940.6422760.03611310.6637220.3803920.7957860.836570.5048230.5210730.5928020.77680.1176940.1497650.513870.02634650.2492970.2233040.5661130.9791370.7544020.3549650.2042010.04176570.6159770.9061120.9613770.8500360.5569080.5362510.9214750.4571210.1873810.1507420.09874380.4308890.9352660.727960.3910860.810110.748190.6950630.5104430.4806450.829070.3302640.7974850.4758580.9079440.5194440.925420.6952560.6508360.6541540.5479330.6038850.2825720.6304680.1244050.9143050.9976590.4733030.9212060.3931480.2872710.5241550.7078180.694080.7782750.9989930.989710.4756480.0929009
990.4019770.4431780.7544120.359530.9221690.5478340.8664730.4706580.3645470.7167140.08160370.7293510.0634610.595080.0997680.4105820.2040320.9396960.6713480.2490240.4650080.6572130.9051220.07292250.0001972070.8348070.5991350.9869330.5931120.5988710.5057310.3420630.2714610.6922580.8965110.7670840.05581110.5907680.3972370.6388730.2638530.07961530.9515610.1653390.9874860.1392840.1901490.4907530.1631520.2340670.1922610.9857530.5354920.08955650.533030.5293090.07202130.2364760.4623610.6610950.4154570.4354560.4017560.79570.6722510.9518870.6880410.1993890.5840530.03315250.3094690.1938350.6194450.3817410.5038810.7852170.205340.4042940.6979250.00818410.5548620.2022430.5696770.2135370.3289690.008194110.4391740.7606090.3026840.4660820.1409830.3285940.8744530.1850520.8810580.6243220.0901490.472030.1968170.225026
1000.9179350.5598270.005645480.820640.4503610.9307040.6723580.122210.186510.7224010.09234820.5638770.7085330.9174380.06356620.03170440.883770.4735670.5371760.8579650.3199920.3213170.1196380.4697410.6315150.3964780.8460730.3038570.7247140.5877050.7197950.9874260.4604350.8856930.5487410.0283420.2889580.8145250.7913070.1755510.9156080.3273940.2363560.3129520.5872370.0245110.8322080.8025150.09878890.9530950.4999840.8601540.8351020.5733150.348740.7956290.07611070.08152660.5968430.5739910.008798720.6693820.1629570.09209050.6622070.2639050.198340.7302470.1115040.9142430.9964160.7019960.6783410.308440.998790.5523440.1132910.1599970.8768460.8090610.03160550.8867580.1132110.3506880.2343980.04879310.6373830.6731840.331010.43890.3965280.5811210.1163070.6196560.7374440.2752030.5791360.1329730.01579860.581863

we can see that 92 of its columns were not printed. Also we get its first 30 rows. You can easily change this behavior by changing the value of ENV["LINES"] and ENV["COLUMNS"].

withenv("LINES" => 10, "COLUMNS" => 200) do
    show(df)
end
100×100 DataFrame
 Row │ x1        x2         x3          x4         x5        x6        x7         x8        x9        x10       x11        x12       x13       x14         x15        x16        x17         x18       ⋯
     │ Float64   Float64    Float64     Float64    Float64   Float64   Float64    Float64   Float64   Float64   Float64    Float64   Float64   Float64     Float64    Float64    Float64     Float64   ⋯
─────┼──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
   1 │ 0.844256  0.0668823  0.567       0.551657   0.118484  0.33853   0.310855   0.945171  0.38507   0.896464  0.433452   0.681074  0.904967  0.00471435  0.682658   0.604032   0.416615    0.776368  ⋯
  ⋮  │    ⋮          ⋮          ⋮           ⋮         ⋮         ⋮          ⋮         ⋮         ⋮         ⋮          ⋮         ⋮         ⋮          ⋮           ⋮          ⋮          ⋮           ⋮     ⋱
                                                                                                                                                                          82 columns and 99 rows omitted

Most elementary get and set operations#

Given the DataFrame x we have created earlier, here are various ways to grab one of its columns as a Vector.

x = DataFrame(A=[1, 2], B=[1.0, missing], C=["a", "b"])
2×3 DataFrame
RowABC
Int64Float64?String
111.0a
22missingb

all get the vector stored in our DataFrame without copying it

x.A, x[!, 1], x[!, :A]
([1, 2], [1, 2], [1, 2])

the same using string indexing

x."A", x[!, "A"]
([1, 2], [1, 2])

note that this creates a copy

x[:, 1]
2-element Vector{Int64}:
 1
 2
x[:, 1] === x[:, 1]
false

To grab one row as a DataFrame, we can index as follows.

x[1:1, :]
1×3 DataFrame
RowABC
Int64Float64?String
111.0a

this produces a DataFrameRow which is treated as 1-dimensional object similar to a NamedTuple

x[1, :]
DataFrameRow (3 columns)
RowABC
Int64Float64?String
111.0a

We can grab a single cell or element with the same syntax to grab an element of an array.

x[1, 1]
1

or a new DataFrame that is a subset of rows and columns

x[1:2, 1:2]
2×2 DataFrame
RowAB
Int64Float64?
111.0
22missing

You can also use Regex to select columns and Not from InvertedIndices.jl both to select rows and columns

x[Not(1), r"A"]
1×1 DataFrame
RowA
Int64
12

! indicates that underlying columns are not copied

x[!, Not(1)]
2×2 DataFrame
RowBC
Float64?String
11.0a
2missingb

: means that the columns will get copied

x[:, Not(1)]
2×2 DataFrame
RowBC
Float64?String
11.0a
2missingb

Assignment of a scalar to a data frame can be done in ranges using broadcasting:

x[1:2, 1:2] .= 1
x
2×3 DataFrame
RowABC
Int64Float64?String
111.0a
211.0b

Assignment of a vector of length equal to the number of assigned rows using broadcasting

x[1:2, 1:2] .= [1, 2]
x
2×3 DataFrame
RowABC
Int64Float64?String
111.0a
222.0b

Assignment or of another data frame of matching size and column names, again using broadcasting:

x[1:2, 1:2] .= DataFrame([5 6; 7 8], [:A, :B])
x
2×3 DataFrame
RowABC
Int64Float64?String
156.0a
278.0b

Caution

With df[!, :col] and df.col syntax you get a direct (non copying) access to a column of a data frame. This is potentially unsafe as you can easily corrupt data in the df data frame if you resize, sort, etc. the column obtained in this way. Therefore such access should be used with caution.

Similarly df[!, cols] when cols is a collection of columns produces a new data frame that holds the same (not copied) columns as the source df data frame. Similarly, modifying the data frame obtained via df[!, cols] might cause problems with the consistency of df.

The df[:, :col] and df[:, cols] syntaxes always copy columns so they are safe to use (and should generally be preferred except for performance or memory critical use cases).

Here are examples of how Cols and Between can be used to select columns of a data frame.

x = DataFrame(rand(4, 5), :auto)
4×5 DataFrame
Rowx1x2x3x4x5
Float64Float64Float64Float64Float64
10.503530.6395380.4127290.2702230.11665
20.7830140.5282620.582540.2654620.0297447
30.2832710.6571760.905430.7706920.168181
40.5348010.7594180.727030.6887280.021263
x[:, Between(:x2, :x4)]
4×3 DataFrame
Rowx2x3x4
Float64Float64Float64
10.6395380.4127290.270223
20.5282620.582540.265462
30.6571760.905430.770692
40.7594180.727030.688728
x[:, Cols("x1", Between("x2", "x4"))]
4×4 DataFrame
Rowx1x2x3x4
Float64Float64Float64Float64
10.503530.6395380.4127290.270223
20.7830140.5282620.582540.265462
30.2832710.6571760.905430.770692
40.5348010.7594180.727030.688728

Views#

You can simply create a view of a DataFrame (it is more efficient than creating a materialized selection). Here are the possible return value options.

@view x[1:2, 1]
2-element view(::Vector{Float64}, 1:2) with eltype Float64:
 0.5035299629434845
 0.7830142455686
@view x[1, 1]
0-dimensional view(::Vector{Float64}, 1) with eltype Float64:
0.5035299629434845

a DataFrameRow, the same as for x[1, 1:2] without a view

@view x[1, 1:2]
DataFrameRow (2 columns)
Rowx1x2
Float64Float64
10.503530.639538

a SubDataFrame

@view x[1:2, 1:2]
2×2 SubDataFrame
Rowx1x2
Float64Float64
10.503530.639538
20.7830140.528262

Adding new columns to a data frame#

df = DataFrame()
0×0 DataFrame

using setproperty! (element assignment)

x = [1, 2, 3]
df.a = x
df
3×1 DataFrame
Rowa
Int64
11
22
33

no copy is performed (sharing the same memory address)

df.a === x
true

using setindex!

df[!, :b] = x
df[:, :c] = x
df
3×3 DataFrame
Rowabc
Int64Int64Int64
1111
2222
3333

no copy is performed

df.b === x
true

With copying ! and : has different effects

df.c === x
false

Element-wise assignment

df[!, :d] .= x
df[:, :e] .= x
df
3×5 DataFrame
Rowabcde
Int64Int64Int64Int64Int64
111111
222222
333333

both copy, so in this case ! and : has the same effect

df.d === x, df.e === x
(false, false)

note that in our data frame columns :a and :b store the vector x (not a copy)

df.a === df.b === x
true

This can lead to silent errors. For example this code leads to a bug (note that calling pairs on eachcol(df) creates an iterator of (column name, column) pairs):

try
    for (n, c) in pairs(eachcol(df))
        println("$n: ", pop!(c))
    end
catch e
    show(e)
end
a: 3
b: 2
c: 3
d: 3
e: 3

note that for column :b we printed 2 as 3 was removed from it when we used pop! on column :a. Such mistakes sometimes happen. Because of this DataFrames.jl performs consistency checks before doing an expensive operation (most notably before showing a data frame).

try
    show(df)
catch e
    show(e)
end
AssertionError("Data frame is corrupt: length of column :c (2) does not match length of column 1 (1). The column vector has likely been resized unintentionally (either directly or because it is shared with another data frame).")

We can investigate the columns to find out what happened:

collect(pairs(eachcol(df)))
5-element Vector{Pair{Symbol, AbstractVector}}:
 :a => [1]
 :b => [1]
 :c => [1, 2]
 :d => [1, 2]
 :e => [1, 2]

The output confirms that the data frame df got corrupted. DataFrames.jl supports a complete set of getindex, getproperty, setindex!, setproperty!, view, broadcasting, and broadcasting assignment operations. The details are explained here: http://juliadata.github.io/DataFrames.jl/latest/lib/indexing/.

Comparisons#

using DataFrames
df = DataFrame(rand(2, 3), :auto)
2×3 DataFrame
Rowx1x2x3
Float64Float64Float64
10.3229010.6377140.819198
20.212280.1162720.0422936
df2 = copy(df)
2×3 DataFrame
Rowx1x2x3
Float64Float64Float64
10.3229010.6377140.819198
20.212280.1162720.0422936

compares column names and contents

df == df2
true

create a minimally different data frame and use isapprox for comparison

df3 = df2 .+ eps()
2×3 DataFrame
Rowx1x2x3
Float64Float64Float64
10.3229010.6377140.819198
20.212280.1162720.0422936
df == df3
false
isapprox(df, df3)
true
isapprox(df, df3, atol=eps() / 2)
false

missings are handled as in Julia Base

df = DataFrame(a=missing)
1×1 DataFrame
Rowa
Missing
1missing

Equality test shows missing.

df == df
missing

The same object?

df === df
true
isequal(df, df)
true

This notebook was generated using Literate.jl.