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
13105385107210
216410171273
3445105131051
469110536951
57557316386
621014611751
77555584653
822981072992
94787892121
10585453101310
111671312853
12764101023465
1331066335324
9894281597914
99047651014163
991108910933363
99253108632743
99310717888381
9948779254198
99584105172521
99610766737453
997299396671010
99897329329210
999109844781210
10005451831988

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

first(y, 5)
5×10 DataFrame
Rowx1x2x3x4x5x6x7x8x9x10
Int64Int64Int64Int64Int64Int64Int64Int64Int64Int64
13105385107210
216410171273
3445105131051
469110536951
57557316386

and last to see its bottom rows.

last(y, 3)
3×10 DataFrame
Rowx1x2x3x4x5x6x7x8x9x10
Int64Int64Int64Int64Int64Int64Int64Int64Int64Int64
197329329210
2109844781210
35451831988

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

first(y)
DataFrameRow (10 columns)
Rowx1x2x3x4x5x6x7x8x9x10
Int64Int64Int64Int64Int64Int64Int64Int64Int64Int64
13105385107210
last(y)
DataFrameRow (10 columns)
Rowx1x2x3x4x5x6x7x8x9x10
Int64Int64Int64Int64Int64Int64Int64Int64Int64Int64
10005451831988

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.6408650.5852460.2370270.1410820.7207740.009685630.06360810.3326470.5368020.1489940.7776430.9937110.4415610.6982210.4388390.4502610.1797960.1889610.7085190.6254390.2557440.671330.1143690.7491090.5261970.4133460.8462820.1975720.3133610.2386270.008247140.189130.6842490.1945210.7808150.693920.2905790.2244510.4135580.7656790.5339040.3574710.8270530.4912130.8292850.1238640.540140.3050750.7850240.4957620.3308480.615760.399770.3368360.04482490.603330.08045320.6880420.8505050.574510.8385090.5740760.8595030.9637590.3195410.522140.1139370.2042550.0203980.02998320.2369230.2537460.3880620.1352580.8900060.1407310.5794540.6737140.2105910.3323230.7326360.837340.7023250.03551160.1298460.5158910.1578860.7181340.6056610.8181270.4964540.5661650.4298890.1349170.9188110.1398360.07888930.8976450.04391070.249845
20.2042170.8928620.1924170.1343570.5593390.3038290.1297250.7153260.3417220.9078720.1310390.7443610.05295410.7414630.1622680.6776730.2981390.3914430.4469910.6288360.2037660.6372820.2347180.6610910.2347750.4189080.4611740.5216930.7093050.6989150.9596890.9562860.04029210.05796260.08795850.8791840.168640.6088680.6799240.5395630.1852660.883010.5082580.5570550.7575870.8495210.06255440.7855570.9414050.8240870.6248890.1165740.9569040.2388720.1700020.4957460.08782710.4904890.4765680.5547350.5765090.5757350.2602140.1218950.5134860.05715490.242470.9092340.105320.6342030.4113980.4601080.7416040.3109660.9234590.3410950.9728610.3879060.1638280.3638950.31190.5303030.3355130.3033610.3211440.03093490.8643850.01576660.7915670.6326440.281170.5942470.1584040.5242880.8029730.7302240.629620.796970.954110.376871
30.2105590.6140860.2911230.6036540.1710790.6368490.4200460.477280.8537220.8382090.7778480.6918890.5679670.9308080.9717180.6396810.980690.2325870.6234840.9680080.9163880.3759430.6837950.1889120.4330850.3716620.8718220.8940650.7306890.3904260.1848010.01652240.5371410.8067230.4501780.3308320.7657270.2912380.5441520.4165770.1373210.7430550.5061490.007835770.8145330.1730930.3687880.3077420.9820810.6782090.2317370.4187770.1475750.9202210.8231410.3483630.5159220.8419510.6971570.8287040.4416610.8827950.2822650.03124690.8114090.6882260.4744640.6302230.3304020.6899590.2467390.4380950.963480.1087730.3722370.1321270.4983440.5097980.3085670.2476860.962450.9836380.8729040.8141080.8147910.1589320.3037930.3243390.3048660.2311390.4437120.1295210.2753680.1260120.3894710.3797170.4985370.0722280.3649120.645088
40.1563290.1075140.4509720.1054020.2810410.7111760.0301180.8854740.5344520.4617510.8936720.4135670.8544860.1180.1905380.1101360.03531540.6064350.1959350.3052190.4846850.1100070.120220.4718550.3571770.391510.6292440.07441410.320760.3738390.9663570.7143180.5833860.2007190.2418280.1848730.4694960.7247740.9804680.7353460.7650490.3808150.2197310.7578990.4339470.7219790.01425570.3301710.5876630.5512990.1751680.139690.2592340.4866760.5118610.007224310.6578580.683840.04368280.3720.4719370.1922460.1176850.2272890.7391860.8366670.6330050.2735640.4604030.3396520.9404850.5441370.4289430.4329990.0180910.8472820.7308330.4790630.07852440.7365340.6320540.9253780.2038680.6606740.3611510.01277760.6417360.9086350.9217880.5115710.7360730.7018340.04563030.6004030.2211920.5093890.564840.2511060.6149480.580667
50.8437610.8273360.9310470.7448630.3150770.02578260.7857540.2695980.3837490.3158150.947830.7588880.4941690.3529160.9829120.1618850.3643620.6152590.1490430.5090.6227290.5253620.9288580.5416690.8088990.6330610.4393760.2625940.5726660.03704880.8045930.5615630.8180220.8612390.9481430.7450260.8060950.5818630.07438090.8981320.4525040.4096590.994030.8809960.6924480.3837670.4395570.5606680.9833150.6945090.1878540.6261290.6668780.225190.3506270.6457690.1882950.5707130.7970880.4788230.5820360.2771770.2319860.01042660.3079850.7610210.3706140.7517880.07797110.4958530.9086820.01331130.004415250.05267630.1279270.5692990.8741450.1552410.93440.6609550.7173550.7624740.5016710.372460.7066620.9014630.3309890.3604040.2239130.142170.267130.6837630.8196560.8464540.7918530.5296820.7260160.4773860.4343510.24757
60.2912980.2472560.475880.1871770.778350.4317610.08146280.6113550.4666960.03844640.9538840.6257540.2847340.272090.8938650.3288210.4659310.7507890.1946720.613220.2210980.042080.3671380.4966620.3843320.5237440.6329370.504440.8031820.1435630.9155370.6953050.007238410.2214850.5344350.6952830.67890.08504050.5678560.5730720.3231320.7235840.8176410.01080540.1164120.9431750.6901370.6312130.008749680.3294520.8171730.5537810.2651530.3363640.4875370.9088450.2552630.02921980.7108620.2276210.3912130.2061380.09229490.01095090.07164840.442290.9862720.4290350.8626750.3326380.3531290.1948330.4438540.3606280.5546010.7349080.2870180.176850.3960570.3932270.1584780.3991890.5994160.342570.5177020.6552390.7269640.6506190.3700030.2982810.9622560.9618790.08116410.06840330.3466660.8028570.9643750.5265210.9642060.92808
70.3914290.7272540.4715340.4655110.6907310.7842440.8535560.1406410.8454050.9867910.02619770.9782820.06105780.935020.9546720.217850.8292820.6057360.4005150.06595350.3506770.2194520.7104670.7267690.3815920.4452850.113310.7577290.311260.01922320.02421750.2682490.2429730.6345560.2546980.9240610.6503960.6064810.09391230.892740.99070.8607450.03859010.4101550.39990.7970230.6226280.7806130.1928120.09233550.02875460.7695890.2876790.7312920.1709830.1429130.2592030.8618540.6311580.6432370.4419070.005807720.884880.2582780.5252430.5369210.04274580.2101640.09890290.03330550.06737110.5409840.5060720.09766480.06937580.7689280.6832120.3021120.9499120.7489330.731250.4246760.7370220.6758670.2897690.5312750.6499270.127130.9161640.8047810.9973420.8160790.6214580.1840880.3166660.7645040.9357720.06352520.8633660.868418
80.6124410.9140310.2895920.7013270.1967860.3690430.2005810.5260360.1080320.2132450.3017690.6990920.179180.5176710.1000460.2656240.1624420.04378980.9629550.8125420.3345030.5801770.7692030.6143620.5643660.700340.6872510.8366540.7821730.3027520.640580.009659440.5195640.753670.05967180.5888730.5733910.8546640.08503320.7262750.9514820.5622130.1705530.05866040.6118910.292340.495350.06561650.3895690.5494020.1916630.3507880.7406390.8629010.2417940.7387730.5540440.9173370.4722330.3115480.8503610.1760910.2894560.01829070.3021680.1529990.8802430.9651170.8116420.1135590.9084810.9547930.2998880.4561920.7581990.2087410.2440160.2991990.5805590.802060.3911840.4067120.1214190.234620.1310140.529180.156110.3011890.6526410.703290.6254260.09257980.9168010.01841720.9049290.03174560.2163570.08142960.2862290.831918
90.7656630.2929010.1469840.3654990.1412650.2791640.598330.3586230.4954750.8142450.3682540.8793380.3381730.9278970.1136310.08803760.3963740.4705670.7490740.2765880.3212190.3093030.5157170.423170.6337250.9567860.6987240.4810470.7540170.02959190.09500450.03913450.8623390.1864590.1904560.2057380.1759210.08694050.222480.0346150.9289070.7800750.4973310.4059910.942260.3200520.2817290.3283290.5720760.3682380.4136290.6535920.8546840.2569120.8374160.7627690.2358440.0705150.06716480.7958470.7918360.4341460.8126780.7923760.6421610.9828310.01432940.6525590.2635010.0006651280.9670540.9370960.6273830.9279840.5980070.3074340.5646890.02443230.8425820.04609940.4512810.7312510.262080.06861480.2308540.2713530.2824510.6741880.3943630.9722990.6077280.3538460.5056150.1448060.5507910.1513060.3798620.4931730.7688390.542907
100.8251990.9018950.4213330.5071560.6523830.07055820.01390220.3546740.5832150.3075270.7335840.8323480.1075850.02185460.7825090.7686250.276450.532870.7763390.9574120.7999360.241610.505920.6394150.98890.7687250.7783450.4844260.5049350.4715060.1652850.7962840.7926070.5384060.7696540.06003770.2075840.9007030.7425730.810070.8543970.2427290.523090.3333990.212240.6672670.521770.6621630.4108090.9535050.6278050.1148940.6205110.5873760.4246060.404610.3351360.8359310.9775380.1805560.4806560.3475650.2379160.9276210.7870370.6254610.387470.7506030.5060350.2746360.3596350.9593250.4099610.1861790.6528110.4394320.9157110.8614960.3163870.8280130.3828740.987320.4492940.4550320.5786430.8029940.08019860.5430270.2241050.3435460.1910050.7712920.338490.8044960.02608650.5909350.3456380.6103020.8680570.481492
110.8237850.7828120.8598990.1823590.7695830.9835110.06684940.1992530.9519020.2974960.5586660.2219320.715020.6979020.9669540.8565220.7497780.7054270.7096410.31060.14080.3073530.4110620.2778730.9450820.5949350.4319810.8085220.3299880.712950.02952370.1679590.03722560.05551060.6827570.5869610.1369730.9401210.7091650.2667380.505650.3498480.6182440.03245220.9167670.6252460.6761350.3347270.1840790.8702550.793180.1960030.7981190.3247590.358730.2483440.2547140.7570840.4664080.6225870.4662460.2856590.872110.3058370.3209860.6998280.4135910.02318570.3221250.5976090.8583010.1544850.9512480.7868410.6136620.3352360.04163230.1023630.4325210.8790020.2333550.5920350.6939160.8950910.6713420.4190550.4407810.3882460.649750.8906670.7231120.820560.4286370.0774070.7022850.7953990.1902220.7306650.461160.219793
120.2978110.08296020.2533110.8354120.1685170.5958180.5945940.1708190.6107570.2176880.2919790.09040940.2257550.2779690.04515740.02723540.07760110.9259050.6649790.7364220.6282250.5946890.885510.1435180.5532580.06154010.7844680.201190.9144420.3169130.02800810.6468270.5995070.4281410.6794130.3926610.6347970.6384240.8149860.01741990.07844920.4130110.9439560.06241180.4731860.5200950.772460.4239210.5947220.8710310.4538920.9302920.03741310.1620730.8273480.9952020.06104170.3937350.3854830.419290.1231430.44370.8697310.5509210.7966660.4386290.1494870.5269180.9121630.9036220.3265990.8224340.5141670.2362030.2840410.7403220.6918010.9309740.5784750.9558140.3337790.7561420.9720080.4667420.5545830.9279930.7594150.005783530.4373920.4846530.8985960.4438930.6476930.2698290.1107660.5480190.5834420.9183360.8585080.403257
130.07546120.5638880.9028910.14720.8985540.4673690.5490260.4166260.7367650.2124990.5431250.1926630.9974020.3296030.3647830.9199150.9931810.7561980.7331490.574640.3647410.07119930.838750.3256340.9436160.2412170.0837070.2034190.1061080.8527820.6363430.1381840.7310190.4827250.9825120.02961430.02171230.3412340.3465010.1538570.9524180.7772330.7728480.8934320.9325990.9668380.8476220.7120710.6716490.3839630.8913190.06306410.03364780.1788660.1999990.4468510.5466470.4609350.5192940.03026220.9104730.8618220.8535670.5531440.7151760.6369660.4638350.2678490.003236440.5816440.3436270.8191630.9001270.5043940.9308610.963160.3349670.8199910.3441320.5629130.977580.3454760.9805090.5696420.4938390.5065370.6106720.8749240.1028970.2223450.9383060.1378490.3755830.6746150.9149140.2918980.5980490.2685710.06639370.582432
890.3196910.7253610.7766480.3931290.1018770.2342920.4107290.4147980.05730210.8647870.9381640.6539840.5980350.1455950.6571410.6975690.6686310.06645050.8817940.966910.02336450.5132440.05607970.2447840.9887610.8960790.3387360.02518070.3541120.6253120.8493330.4356320.4877830.2983650.5496410.6996950.7834020.6019830.07494450.5083690.5841770.9560080.5701170.3159510.5909810.57660.5586040.0003283520.003461070.5406180.3839830.6574410.4257820.4716450.3631650.5783930.1899870.6399020.7363120.03503410.4652460.3054970.8672090.5361130.3070320.8143280.5154280.9462720.7270640.5657920.2398660.3369560.9842320.1713250.1539420.3068340.927860.1263710.1029920.71530.004048630.1524420.6440420.9400030.6850570.3297960.3758210.986470.8022410.2152120.4276260.5244180.7592810.7990430.1185560.3023690.020720.8326310.4269870.733532
900.3001890.1008670.6230590.7308050.5715820.995410.7897150.2131720.908920.8368340.9884890.9259710.7458810.7137060.0416870.3229340.7552760.2235230.7183280.8489020.6701520.3755640.6552140.01817130.7396780.9378540.5171340.722140.09473960.6108710.5219310.4264920.1824340.2459960.7038370.1774660.2774790.3417130.05592810.4595010.309520.6406230.52060.3427320.1402520.6323110.8069480.873920.2275840.7847240.07652990.477930.6900930.1131010.5923460.1885320.5168690.7104420.119780.3472430.6031240.1174960.5975250.4343520.8697080.2187320.4525680.9940180.7122580.04517250.0497170.3442940.6129830.789040.4681740.3103450.3778090.247560.170680.982080.3933180.2465710.6935750.6697140.2620380.7536280.08661870.9578430.348730.7660720.8216450.9360180.4874010.9376660.3389770.3388540.8068780.4837410.3837980.855389
910.02351150.9666450.181680.1611410.3301450.9116860.5270030.202230.07064980.3849440.2054830.5387360.1388240.1887970.3752870.4654090.003413490.3021780.7927520.6592720.8551550.4304840.196740.01342880.3668990.05068480.6380990.9440720.8211330.1064390.473770.7201330.9061130.6573510.3966850.6656430.06422840.1730010.8766720.7046110.8531420.6660490.2873530.398050.5313540.08497480.5261810.9615690.5628070.1490440.2962920.2055930.5089540.3709080.3497580.2837390.780980.01172790.7216860.6770970.03381480.8371110.9642020.308030.001617350.2148640.07178320.8220990.1694510.0564530.1816050.4549540.3043510.05799160.7251270.1902270.4840350.6757820.7714510.4042820.5621560.646310.4625450.731480.8072960.5008280.02140180.3615840.3339840.04759660.2590860.5049710.9400340.3522080.9141680.2953780.316890.2399110.4733550.89399
920.4623360.4638820.3820940.8298570.8914770.8451130.7263550.5738460.3002230.9790540.08835490.9106920.1605380.6637810.4714120.7122370.2060240.5452270.4203470.4462750.07543710.4126350.4247270.1902670.9560140.2859160.8148610.2445460.2156440.4928420.7245750.314680.3684990.08959540.09410520.6995040.4523660.37110.8682390.04319870.3946020.8783770.4984480.6167050.9422070.3618680.5057640.6285050.944520.4677840.2106940.5737960.09783180.06957480.675340.9625120.4360760.01874580.1964490.1436040.3957550.4861930.04415140.09791670.4363260.02084160.1427220.9176970.8065410.007952220.7835960.256590.1817740.05842490.3386930.3468380.7018330.915140.944390.1642970.8526370.04843150.986710.9584260.5091240.2424650.4868750.2392840.9221390.873260.1432730.4445410.6799720.6072150.4210350.4143850.5024770.06604430.9513320.20954
930.2495260.7976970.2655450.3441430.4937240.5561310.6660890.7785180.1703840.9035380.8974490.4300340.9530760.566270.8841920.7626620.4826860.454250.04661040.6333880.9153450.9699760.06095090.6784390.3797450.7036940.2476990.59920.887840.7112920.5078670.3159350.4740440.666590.0535620.8526580.2199920.9045810.07222310.9245780.633760.3799050.09835870.7388190.03095640.5185220.9784660.388150.4860260.03688570.8792410.4611830.265420.3608110.4571670.1227350.6796110.2567990.3231420.6684660.8180140.5535040.7575610.6614570.9245430.9600980.9342170.2227020.322870.3089270.5215480.2020590.1451130.4759370.2803550.234380.9720170.672510.9830580.7023950.6925010.851080.4018680.1058530.2591630.7464330.5331620.4865270.7961410.6225180.395790.2051760.1399720.3766160.7467620.1215910.5772760.0995650.2235020.462456
940.3464950.823170.3670120.2727160.85250.2688430.6380490.5641550.2682430.7910140.4783750.8779040.4946820.008852090.3467620.9968720.9393490.2949440.9156140.3526470.1908780.7199810.5667910.3864390.2065820.8360540.6310760.2983910.2300250.08170420.9649210.5887240.6710830.2838470.06357720.3794920.3232870.4765210.9705220.6750510.8983410.6981060.3284840.2101870.4124240.05391440.5478830.9658660.6583510.6574960.0166780.01089070.5070590.2217170.1841680.7458970.2440490.5164760.260170.4963440.4980390.6545280.3071830.9559610.2129420.4005120.1864150.9054220.6202020.4289980.4678580.1320840.9835520.2339020.2883590.9286810.857980.7805340.6478610.4336170.7710820.3852770.3686530.9055590.07764690.3916240.4284680.3405940.9771930.1418520.8985790.8339320.2579930.1581710.5932750.5478950.5531340.9694230.9143740.795725
950.8112590.3031430.5763440.5778620.3649360.3968790.7358140.3410160.3778730.1788650.2019770.413450.4319340.1882040.006767850.6486480.2503320.03424760.6435090.8895240.7121960.946760.8360620.02678110.6972040.8984110.5911830.9993340.5465040.5866740.5133360.4829350.1315320.9256490.3872880.1514470.4791940.9996350.207090.4463730.4485330.7416310.8392740.05727540.3058590.693740.8137410.8656020.6630490.7059070.8201410.1899560.8472750.5517310.5461810.1210140.6900020.4875420.3849340.4339240.5397040.5947850.9380610.26710.2718710.2850240.318470.2474270.6373480.5824890.09975690.3505760.3305880.4098430.913010.4899030.5496580.1124770.5936620.6601280.4382550.9908740.5640540.9817160.5143230.5943420.6296870.9510110.5956380.38920.3350490.5849280.5643260.5398230.02494810.8945240.5373160.6151850.3201960.533406
960.1922310.02045580.09168510.7338440.1769560.933280.7704820.2470540.5776290.5427130.737930.6545560.2654980.4175020.2266630.4163460.3822410.2810970.01531250.8818810.9665610.009138230.266660.6144480.424340.69310.8318040.4891070.5475410.6543560.7423260.1392890.04534890.06014360.4916190.9659350.3843130.3088230.1793280.05688860.6544480.4355740.2131120.7008290.03651280.4697080.4899620.5603910.9262640.4714630.2198880.8769980.9581930.504940.8820810.1652080.3829650.9637280.8258190.3476580.8985840.4418770.6053310.8662390.1232780.2268770.4490180.1732720.6521210.9896780.665070.3025240.8223660.09213380.9318680.8466070.4382020.7324080.6864670.7882610.9881170.3942170.7856610.7112240.1599390.5763930.2283570.7009920.2957640.1753810.3781260.2069190.4737340.6690070.8342590.7269380.1457310.5306640.6630540.694132
970.1453630.07090180.6692030.4917120.7022830.8621290.4787220.7349740.9687660.7331780.236190.2062930.5278070.7116930.7189260.8009930.2635770.7677930.42180.1099760.123190.4700980.9688470.1300430.01497970.9416670.419710.7266820.4677650.5655620.894160.9191030.9842180.6742910.8855570.6767160.5403410.5999730.9004470.746210.7764430.03288580.08629590.0994880.782580.8804230.3957550.8177590.2635930.3926430.1033230.04032120.5691910.486860.3185730.8951860.6580090.9867920.003224030.1411660.4987150.4021640.6941090.7484240.7140250.02029060.2947480.3693750.9894230.1106070.4849990.5140810.1428440.33060.7617980.8664820.871440.2819190.833490.6234250.9346510.8112540.2082120.4632340.6494350.4941440.2097140.6251680.5960780.4535440.7164770.6565340.9815070.7861450.8275250.9543950.1426790.6408580.05664020.211164
980.6306330.8974610.2976560.1957890.6733790.5583710.2851820.8850760.1155690.2545210.6376720.01356210.3278280.2810770.9231050.5273380.5226210.1813120.1527570.7166310.5448180.8422190.2704760.925370.1447520.8977120.1877360.7245250.153270.230020.1549620.03406520.2935380.0433370.8249110.4404470.07616140.4251170.05281920.6683030.8887150.05065090.9649990.1109690.2005130.5627640.266840.140020.5184490.8551850.4099360.4061410.5256920.06659660.3078940.5744340.8182330.1863140.4754510.07081040.3276070.7271890.2263940.5570030.990720.5779630.5864360.06790230.5512690.5968810.898820.3581150.7159480.3953390.326730.6458830.3748890.02654040.3061370.9472290.7372940.7653410.02225320.7161120.3143830.5139720.6501210.08623630.4653180.9360040.4796560.9487380.09291360.0324950.7461710.4033140.6072760.5527560.7996340.664459
990.3120140.509890.0800470.221090.8406220.9043310.5780910.1088130.7711010.02927470.5622130.8822150.09544440.2060770.2513270.08263870.2200490.8055250.063490.5272170.5092320.6507760.7286030.1283180.2796540.9417870.1683660.7838150.3174640.2317730.03410050.6981740.0717260.506550.5877510.7024090.6593530.9907970.007500740.6241730.01319840.3967620.1973830.7832550.1138540.764060.3839120.5624270.8324720.5969950.2645210.3325410.6299530.2689250.6240130.3541450.3149010.4260170.4856560.5204670.6021570.8508720.9011910.209370.4785910.1291650.488420.2703090.2066070.8627610.190870.1497070.5383670.2705710.902170.5894220.7142780.4736060.5712030.8363470.4199850.37890.01580450.3554490.3427760.7037560.9661850.8752120.7968680.4615250.1765440.41270.6081680.2046860.08149140.4398170.4066690.1949290.6078280.991723
1000.1981290.5924270.7068930.9109490.6621630.9606840.7583650.0970080.4417420.0350330.1997910.7715090.1931530.3833180.1484430.7886640.9846710.6190770.08495740.4852990.9789660.8000820.9173460.9047470.5833120.4548390.2385110.5474690.3852280.5307420.4610370.2749110.3004040.3033220.758020.9152990.2102980.6809910.2096570.5915590.2685480.2217310.6506160.2905360.3914650.6374430.574890.9339210.03441050.6713940.4259960.9665310.6953320.04133790.8983210.3412720.2267740.6866260.3449590.02115720.8104560.7903110.167270.4653290.9824780.7781630.5735310.155220.8483240.4604990.7531860.1909490.04765270.9085930.006670840.4050240.9221470.2793370.3991490.9130070.59030.986280.6492190.6565330.4921290.5409710.5540260.3003940.5025230.6697540.5751110.08569780.421430.2921240.1314740.9529470.9208880.624890.5448420.31638

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       x1 ⋯
     │ Float64   Float64    Float64   Float64   Float64   Float64     Float64    Float64   Float64   Float64    Float64   Float64    Float64    Float64   Float64   Float64    Float64    Float64   Fl ⋯
─────┼──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
   1 │ 0.640865  0.585246   0.237027  0.141082  0.720774  0.00968563  0.0636081  0.332647  0.536802  0.148994   0.777643  0.993711   0.441561   0.698221  0.438839  0.450261   0.179796   0.188961  0. ⋯
  ⋮  │    ⋮          ⋮         ⋮         ⋮         ⋮          ⋮           ⋮         ⋮         ⋮          ⋮         ⋮          ⋮          ⋮         ⋮         ⋮          ⋮          ⋮         ⋮         ⋱
                                                                                                                                                                          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.7690920.1575610.8657520.8456970.483309
20.4879820.02112380.1161260.2000340.275969
30.09927060.8179880.5022760.9487670.377813
40.9976830.3215580.8161590.9686030.582501
x[:, Between(:x2, :x4)]
4×3 DataFrame
Rowx2x3x4
Float64Float64Float64
10.1575610.8657520.845697
20.02112380.1161260.200034
30.8179880.5022760.948767
40.3215580.8161590.968603
x[:, Cols("x1", Between("x2", "x4"))]
4×4 DataFrame
Rowx1x2x3x4
Float64Float64Float64Float64
10.7690920.1575610.8657520.845697
20.4879820.02112380.1161260.200034
30.09927060.8179880.5022760.948767
40.9976830.3215580.8161590.968603

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.7690922633183142
 0.48798153284543044
@view x[1, 1]
0-dimensional view(::Vector{Float64}, 1) with eltype Float64:
0.7690922633183142

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

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

a SubDataFrame

@view x[1:2, 1:2]
2×2 SubDataFrame
Rowx1x2
Float64Float64
10.7690920.157561
20.4879820.0211238

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.6906330.2902330.340296
20.9838220.9093590.820977
df2 = copy(df)
2×3 DataFrame
Rowx1x2x3
Float64Float64Float64
10.6906330.2902330.340296
20.9838220.9093590.820977

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.6906330.2902330.340296
20.9838220.9093590.820977
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.