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
1108294923109
210886215877
373673337810
4106841011885
511081091010744
6109107256197
75975231799
83915632946
91725888384
1039671910222
1163952410445
12211958391010
13910755710844
989274931110107
9904148388239
9912839678591
9922959443537
99357108394637
994313873210610
99510498387664
9966117231398
997211069110421
9988632686237
99910398148731
10003324598116

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

first(y, 5)
5×10 DataFrame
Rowx1x2x3x4x5x6x7x8x9x10
Int64Int64Int64Int64Int64Int64Int64Int64Int64Int64
1108294923109
210886215877
373673337810
4106841011885
511081091010744

and last to see its bottom rows.

last(y, 3)
3×10 DataFrame
Rowx1x2x3x4x5x6x7x8x9x10
Int64Int64Int64Int64Int64Int64Int64Int64Int64Int64
18632686237
210398148731
33324598116

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

first(y)
DataFrameRow (10 columns)
Rowx1x2x3x4x5x6x7x8x9x10
Int64Int64Int64Int64Int64Int64Int64Int64Int64Int64
1108294923109
last(y)
DataFrameRow (10 columns)
Rowx1x2x3x4x5x6x7x8x9x10
Int64Int64Int64Int64Int64Int64Int64Int64Int64Int64
10003324598116

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.2842570.02057240.8239520.4283490.4956710.9369880.2112650.3179210.2872440.482680.9221290.2281330.357260.9030010.6839510.5783390.633420.2295440.4011980.548960.09175360.2882190.7335390.5413740.4180640.08562760.3007370.06655220.8327350.08373740.7481120.3324130.3996440.5505940.117530.9939190.4761890.05020030.09719470.4439290.8749390.3809550.8052680.2987910.746690.1209960.2610380.6841830.702630.5564860.7190920.8005060.07446730.03882380.226610.704220.6169590.6026320.1676310.1645450.5983730.8322730.9728070.7766950.4868370.3557990.1712730.9081370.8725110.8259990.2956510.4433710.2807670.5768640.7410220.4894970.9485010.7668290.9062530.4549560.4435120.7743530.8394410.5719560.7930949.94405e-50.7196370.2660670.5947110.3922730.1264070.5814580.0003584320.6992630.3837580.4572370.8411790.8817060.3317570.776884
20.7599520.8963580.6533060.677260.06309330.4103870.8323730.7232770.2675290.3827910.2553060.8294080.6664040.6894520.07763470.1156350.9160810.548960.5607230.007101580.7636680.3811580.1762160.1154290.1212530.7769690.6572640.4587790.6839720.5205780.9492490.8762880.305190.8947750.6566910.07713010.06857370.6956240.1075330.2867870.6822240.230190.06047960.6814110.8249450.6143330.7134730.08597560.04117630.6512420.04507630.2979710.9238860.7056330.2305610.6001840.6974770.37070.6348290.965290.3673240.9334910.9163130.2707040.923670.8332720.7782490.3955540.8200590.6228540.4564850.8123130.9055770.09232930.3542110.8509070.1940210.6484640.6500170.4398310.4672320.507370.08113890.8484570.4374420.05619420.2566220.3574150.4147480.7963290.3490720.1005140.9196110.8779230.4085110.5459340.3832510.6518840.2996020.372291
30.8993820.54340.7825650.1141820.88120.7311390.8873320.2858790.1489060.1108020.1757690.2659480.2605030.8129630.9374340.9559970.6223020.7758140.3196050.05030430.699070.7355260.8944090.6329390.6853350.6856570.6756840.2849720.443460.2434960.835580.5964630.2525620.8165460.03083060.07391550.7168510.1419210.6656850.5454960.1007450.5102510.988490.0114420.6549150.8655470.09032590.7661370.205120.874540.3232620.06481560.3772320.4706610.01270590.9089640.7959670.121320.484420.9644530.6618120.4899760.9160590.47350.3688450.8370790.02749710.2126610.5299640.4674610.7957140.120570.8636260.2016520.4975960.3865860.9882830.9022390.4692240.650710.7974650.7235740.9911450.7771090.0629920.01669510.1879690.3493230.8047450.2275030.9883530.6301540.3028710.4899150.9774180.01135540.03058660.6075410.9493570.845277
40.714990.6657960.3541810.6096990.9126090.8647530.8305560.419110.2800820.4468740.5598060.3509450.4600350.2296470.7552420.7568650.5636960.2003690.8795930.2104980.7535670.169650.2046210.9343410.5366410.8968010.5055370.3505990.5268130.2034580.8775510.04039110.7395670.7568350.9353540.2846340.4619130.8257230.5109260.1689770.7237260.3848520.3069880.03711930.8917840.2530610.9422070.8445410.58280.7251060.3760440.9539740.5167940.986050.2617670.1263210.2389040.4383890.1164010.3470170.03068590.9702490.6180080.9219130.5847460.9051120.4371350.1046940.3931530.5967770.5002370.1074270.8122840.5226680.5597760.882230.4608880.2593520.3999910.5994860.1316740.08765250.7685320.3322820.2932720.6651040.7594680.9172580.1833750.6954060.519430.1857970.9217550.858180.5829830.7143660.3340960.9891280.8011240.729409
50.6512360.1983720.273360.9904930.1809480.9288060.2286590.6577610.8439050.2637010.8105090.7873020.1010070.5909960.2311120.7945380.3602370.8088790.7849190.6488270.2313880.5751690.1486760.4308520.9565960.2756390.8995410.6111880.09971040.6105610.5038490.6415970.9876470.9643460.3797080.3823820.411730.5053930.06473190.4614340.1924650.5201810.4444040.05505340.9354890.5784240.6016790.5544950.4223360.0508930.8108570.8717080.5903540.4409810.5834160.8825220.74390.6679240.8911270.8451820.7739080.8412520.9214820.1746940.4583160.3297540.4798420.7392680.7148140.4891670.3416940.4325010.2327790.4506390.4213010.9242720.8855370.4578050.03465170.3273150.5292210.4944310.3755610.3891910.5921620.01649660.5041780.680060.8381120.3880460.5734610.8909810.9550640.4850030.2813250.2359660.4280190.247480.1097670.725201
60.7159980.1430160.05864360.4642660.9958650.0463820.07641640.4349880.9605750.1708960.3803650.4078560.4669220.8805420.7179210.710950.6155870.8797040.9528470.9217760.3270790.02465840.1680180.2451810.6403990.02072550.1009650.4190660.07390770.1740080.1710920.4864660.7319670.6652210.5917920.5694790.862670.194190.04677420.06982320.1534390.7172240.5907820.4166350.07469890.3970820.7745110.06230380.8444430.2445920.5196990.02320240.1936390.76660.3173630.1753950.08839670.8617620.333110.1509120.7607250.5501310.4279590.3415990.3973080.4825270.7140950.4361640.2415360.1723670.4581510.3254140.3238850.5759060.154890.6907010.9009070.4944450.03106370.6349880.2285790.9783940.3633720.3897320.993920.4159830.6341090.4332160.301990.6797510.7348360.4257670.4767780.5822940.9921480.6114430.5374070.5109840.3680250.954558
70.5420790.1972460.5154410.5185740.9100360.5544720.6764670.5382480.4215280.8439490.2246220.5554610.9150330.7220350.1592020.974250.9582780.8778770.502140.4174410.1418280.629470.8369210.0149540.5929760.9217460.1774440.468870.3726290.1284980.734230.4525220.9320140.9299480.6772870.02405450.1365950.7134280.4918830.7799150.1107090.6367790.7815110.2437160.396520.196140.3017530.199850.02089180.4004030.08353680.08715980.8153620.1178230.1977820.6224410.004017680.6205680.7978030.1702350.889220.7603410.3766250.2243640.4801050.1069720.8567640.4854720.5436150.7124890.5317140.5642190.3949820.4050870.6557220.960830.1594050.818880.8061340.04963020.6790110.3908190.01110370.04849630.4157830.766240.1427390.7754970.4814420.062370.3861450.9124880.3469130.2074920.316540.07241870.1656770.2563980.6934850.95161
80.6888640.5295210.8861140.67330.5386580.3982540.800190.703460.8918920.4419270.6648530.120220.0451730.6590590.07591840.2378330.1489660.07326930.4115010.4184870.6256610.7213260.8347340.6164590.9727050.9195760.2670750.2908360.8240210.6078840.1496610.1518760.3034840.9518770.362630.1927580.8001690.3324910.6641130.5472190.6689130.02372870.9289050.9394810.212350.6974420.5090850.3438450.2890640.5926190.6580440.3916810.1360270.9391430.1090280.1003160.3812730.8641970.5925660.7243160.7633680.8458730.3838570.2872510.2239760.568880.08614860.7569960.0807920.3698380.4359190.9116690.347150.4919220.8148580.9197030.4243610.1108750.1709750.4577110.2297040.7872250.9527250.2812590.8980750.765730.2550210.570230.5396140.6780620.5796860.2693490.8474610.5648360.2986130.1016740.475080.04523230.8767610.232639
90.6566080.8810970.2508020.7530660.1975410.703490.2721860.2938090.348210.8065010.1125730.5625430.6092990.1658450.09286910.7708070.7121490.1727640.04393260.02168030.3272960.8350940.08916470.1665610.1161910.6932020.1063160.4802160.7735190.7154750.8787430.467920.6483080.6960350.1682710.4018950.7856790.1190180.8562990.8131920.5366330.7213120.280310.3425520.3479090.3332650.7204150.5816640.2921480.2036530.4292190.1833640.8961390.79480.1744470.03972060.4798450.4609790.456620.4505530.9025010.2147950.02017160.5558780.674820.6028110.5812360.8005460.8542670.4795870.4315940.7928010.2386540.3739070.9707240.3600890.8926170.9603360.2664790.8133550.9489670.8732540.854270.3270350.5969290.7208180.6070590.2519610.03621010.8045220.8356280.8956440.2089220.9749990.7865060.4368530.8421490.5276940.8050360.861147
100.04237080.5775380.6904730.5023330.5971430.9755870.2947220.5660180.4527980.6883070.2080340.6939430.04382580.6221270.5079710.5118270.9892650.3187430.973890.9277720.8658930.0592220.8811930.6120060.133760.4182610.6848320.4793990.3052750.165720.06054930.3444440.08023490.3820640.906510.2940390.5008340.3107470.8849150.1058270.6138790.01602580.3416460.04439390.906470.6022550.6405820.1817730.82480.8206710.7599560.658280.06274830.9784960.6224950.07162910.2691570.6324070.6655140.6260870.5652540.6509920.5516110.833780.1438480.2130930.1603120.7540010.2877990.1718840.7882810.227450.249920.7087870.6923040.3318310.06840140.2584580.8327190.5285220.2476240.9967310.5409790.2521420.328430.02825370.9351370.7127380.4510990.9590930.9747650.3847750.8136850.3712160.7443710.419790.2812740.569930.4817680.00248399
110.8406790.6693920.3166060.7770520.3819910.06951540.8444040.3075720.05033420.2374220.8556330.01139730.4806070.6903490.6604090.8539560.02651270.003758620.542620.6151130.7410320.6807130.3151690.3232380.6077240.09002780.6353130.904230.6188120.3336650.5472480.4591910.4090420.437680.9227870.7844970.502410.4567080.9908620.8740440.8237820.2112840.9430840.8982080.5309410.7276610.1462920.9118490.9477890.2578430.7364260.6762030.5163810.1918780.4056050.9368840.5771970.4955930.5639620.166110.1341640.4586850.4989330.8825380.3725840.6503170.2673440.718360.9541810.9518920.06392980.8738240.1313120.106090.5852610.1862180.302770.4671940.1251660.05515020.02348890.6132150.01138510.442250.1933420.09336940.5301480.9955030.1560620.1200870.099260.3425730.8364770.7457140.5142140.0508930.8364510.69990.3945010.42207
120.1321460.2647230.155310.7378190.9511840.5020550.0813560.6268620.9297550.2776730.1579490.4045080.2207920.2467290.03666580.09510010.6750170.5737270.02835040.08588870.6845980.560340.9599880.7651220.5827390.6209060.6160910.4307180.492530.9649280.3925110.5291540.01564980.9622930.8938340.2284070.1134690.4232090.06986810.4772650.5128030.9011490.07689850.9049280.6001680.4624930.8402930.2271740.358190.2203170.2110360.8862630.6697450.7039620.859970.7398220.03142450.5690430.7655640.1258850.7150710.4822580.1221380.5167390.3515520.2462480.9193720.2593220.6683890.9326240.8040150.2262770.1859730.644410.4605960.9663250.4055910.538360.9918040.04795020.1265640.1201370.2222690.7171580.07592240.7218880.6191690.7498760.1137620.482240.26010.4009720.6130160.3088510.6517260.823340.6762630.617250.4395330.840251
130.08955350.8693450.5461820.9250030.575140.5156760.1740280.7918670.6539440.04729980.8763010.5784180.5603530.5488010.8005270.01392080.6251950.6444440.05039880.1965020.2179210.3425480.5017180.7161020.3573780.1649710.2872810.0104880.1049990.5643880.9946060.5618090.4031610.4453430.6412160.1587480.08369510.7849650.7031220.1739550.8208460.3804410.9865560.566690.686460.3701460.7789590.8431830.7315750.1677980.7561350.709210.5246250.6463730.4011860.1369190.449780.6444020.05918610.5698350.2269380.2402210.7071450.8498350.2801010.7311580.6649520.1973920.6911050.675790.5862440.1413470.586370.5696680.7494050.4112390.8759730.689730.7455140.06824520.5791230.3069830.08200450.2951220.3225270.001747030.04551480.1349760.2672110.2709390.4141560.6897660.2473020.338210.4062910.7359160.833590.967410.31120.711926
890.6677580.1942250.8136710.07709050.9973640.9512020.6628970.03312590.04406790.5270740.4857770.3819170.6530710.3309450.2448870.516340.7967050.4543290.3662280.3839670.2081580.425580.2924960.8044740.9793250.5152140.001570390.4594810.5643120.5742540.11720.3379180.5845790.9221650.2487390.9654220.7677420.4108740.9490520.2020640.2921650.7082770.3607820.3922890.6830530.1864620.7181330.3953380.9400370.1190770.4301360.3637730.3519710.9781750.1268290.1661610.05076370.5299280.01540670.08183340.9565050.4543870.7953410.3749130.2448940.3688680.9096130.07646290.048440.5466210.3529390.2919080.5693640.9544910.2831070.9324980.1323170.2545420.7320710.2492870.2207920.7855480.9270650.8686550.2551810.5134160.5521960.4744670.008720670.03731760.1230490.8627380.5751860.3606430.3306470.5343680.00272710.1524390.06793460.668618
900.9455850.8881430.0171680.5230610.8849020.1355250.781210.6397070.7395520.7678460.3945590.1032390.6779060.09246890.4070960.6726410.7101520.2418280.05818620.3548240.4362940.6150990.2221010.2528730.02870580.5427810.224230.01656050.7900970.9492350.1911220.6358010.9990720.4011660.6097370.6126770.607710.3229570.1059380.0745930.5467640.7177010.4106530.4873440.03415190.6429910.2815410.3890960.7579270.3463140.7893920.3461240.443880.6947780.2664170.7915520.4177340.3145690.6350750.6939930.6506380.8215310.1697220.6146680.3318890.08110030.8769990.4129780.8830640.4690120.5556360.8049070.8069850.9844770.435960.2522250.06943160.5188950.3484010.6153510.823670.4760590.1408380.7977010.4997330.1779010.2198370.6467330.8525220.7727060.05406170.1588760.2934120.177210.2431970.7108290.9490030.1107040.8346820.130553
910.6435930.9988260.3459840.2620520.5298090.9656970.5182120.3758280.5840130.819110.1436030.8497780.4556860.4584680.3095710.5527480.1260410.0617830.5427930.7030260.3863640.05476820.07622710.7093240.69870.6431030.5382070.3242840.9910850.5126920.3513980.3422590.6489820.176580.1255190.5876970.7253670.4380010.4796490.4335790.8645970.5041670.4042740.3280310.1097870.5534860.04295220.3517890.7327040.6722650.7576920.8810090.8162070.4095150.6700360.03151380.1261230.909820.1884010.8276860.04899090.760710.8528110.590840.1786520.4148990.2615450.2675040.09220940.4443320.3032190.330730.4805570.2934440.493450.9445970.7396130.9618580.6668990.920610.9848430.3567520.9146780.05291160.3458280.2223780.4541540.8320410.852660.9286630.7486180.3053960.5162930.6672850.3857140.8318910.2294350.4253050.1426860.775165
920.719110.2983610.7826160.9501920.0548440.9191190.9329970.6777960.2509040.492360.2973290.7362240.1310110.9108940.8823920.5755630.5305380.207060.8455440.04658030.5672790.3390640.5101280.3640490.6509350.5776040.6644650.7694010.454060.6649640.2845890.3652140.6111760.6489280.8810920.7628770.6551460.5563420.8095690.05613370.1521280.2117960.2891570.8110970.8667680.7480590.8311840.2798470.9778260.12860.3718730.8541410.8619560.3632360.9570050.03869410.1998390.4735690.02422170.9114460.4828280.2941560.9133980.5395240.2278780.1493180.8469080.3782990.0542950.272490.9204060.297930.8910820.7077470.06943220.9015690.0482510.5104720.4152760.8637390.8484410.5147630.2671740.7935840.344660.9157940.5910460.3551710.9280070.1116820.02427940.8098040.9907420.4757010.400030.7961740.1771210.5218240.2568970.0586097
930.3044410.01315080.7113050.8628260.6222370.1074430.5055560.5249180.1263790.4206610.5359570.4242310.5145550.8780040.1079190.7920250.01280990.2750380.9208290.39250.3418010.7653690.6538170.5171790.2432120.2999930.8300260.4894980.6295440.2864830.5609160.4002340.6891790.4801790.5249540.0343330.427990.08554180.2751840.8709450.6227010.3936990.4603770.8154420.07062180.4762550.433510.9545540.6091250.0238230.3577520.706220.07931410.8635930.4476310.9280450.489680.190310.8347620.4017690.6481450.4787690.6580480.7962940.976430.2316680.2947460.201910.471910.1569370.2351060.02214330.43920.9388550.2513090.5746430.4578270.1031770.441850.3914630.1279040.430530.4563640.9645070.2690010.2066760.2264210.7163590.4209470.9295740.6614550.003031280.3035560.5612580.2248720.8912040.8115660.06424110.5917010.764462
940.02290620.5369840.7418480.9297030.3525110.2139080.9293980.2881930.8832130.406840.1294110.2718830.6787160.936140.5648840.6226850.8793820.7591020.02622180.1315450.9424220.7440390.4787580.4361550.09431330.1661220.1062040.6574380.7872970.4778530.9305820.1803540.05786410.8894490.7195450.5797710.6102360.03432680.43510.5850730.9234140.4922850.0613830.8423870.5961390.533020.04189630.9035860.4627670.2815850.9822510.556110.4742790.2562870.7986760.1492320.3405090.9604290.3867240.39430.5614110.04289110.3478920.2110820.4046670.4876030.8758690.2914730.76960.518640.8946710.2592560.2415280.1442880.6860760.2585890.1939330.5131310.5134550.5700120.5620860.3740810.02033710.324150.293580.8835090.9912770.6622910.6077290.4149720.3257310.7846670.3281050.8633610.891560.6654590.3370430.7944110.3965260.0896399
950.7492330.8627950.4918160.5463260.1846780.1706690.3657190.7460810.9117140.9529250.09165080.438580.2044730.4732270.6472020.0201920.7902940.04548420.006972320.1724530.20.4299140.8551770.1352880.9451080.7936080.3230550.6181550.9330520.1810850.6367190.5752140.3676870.7732510.9728930.4077220.07887310.3410070.4852710.7834590.1928430.7511890.02324820.8799150.349390.2471480.05198260.4772430.9524620.6616540.6983750.4773530.9008860.4221150.7940650.7773340.5923610.9912420.5520520.3089250.296990.9169630.03957260.9438970.1107370.9513090.6779570.4396230.5642840.5753090.7852850.9421350.4237370.5614840.2592030.6948660.2610220.1840620.4907550.2650070.8908210.9386540.7423220.4661950.6029340.5169730.7111540.5082290.02719070.3148230.08558220.2875540.3229310.04703420.5928070.1885460.6503180.8650910.2126430.246136
960.4805930.4498660.638080.5206280.1806830.6089980.7977380.7529310.219720.3254260.03809060.1127580.6214880.7939470.150790.3497090.6864420.1225690.7916510.364770.5613370.02079070.02207850.4545890.08322690.005685540.7796980.09766640.9503770.6790820.9273910.6040820.9647630.6173650.5232180.825330.7873820.8127860.1405410.6195270.9152150.3072170.3397140.7684990.4147080.07891580.4756880.7171220.6410110.3450980.6967360.189810.9142440.5845470.2696390.4653870.2705130.4948470.7489460.1714530.8127670.1436390.3374980.9984940.7889910.1619970.06374890.8965270.9289580.9801370.01071890.2992840.01191410.5050190.7775920.817190.4618330.2032780.8787340.1374760.6370020.8831840.7435150.345730.2290270.6424250.04206320.8760970.3111920.682490.1273260.1802210.9361190.2283370.9287190.1191990.8451230.9538880.2441120.296257
970.9210620.04776330.5920860.9820050.6051130.2060590.7249470.8933580.8755910.05211970.3618770.5325850.5514630.5309650.2754480.4200960.4217310.1956010.4046330.3932740.643950.4253970.8583380.742250.3305550.1832610.5984490.5985510.6043520.8285170.9635320.4461040.2160450.2818670.9965110.1003850.5419910.8492930.7902710.6521910.4179540.04299370.03876290.438860.9500060.6369040.9219090.1679170.4654370.2817740.603670.02851410.8596140.3684610.5638190.3942350.2253480.2116590.9188310.07041080.09904370.8690560.6719840.9326710.3805260.6937070.6842580.4780030.8364790.06895740.2078390.2130470.4543110.5936750.5215680.6697550.9716980.3890160.6959280.3615810.1211820.4817110.1695880.281380.160570.6057420.05782690.1764310.803850.1505630.9325330.3137080.856770.5619180.2609220.2181960.2863050.2259440.4331170.459442
980.2007710.6411380.577950.2696790.9393280.875250.9155980.7037270.8000520.3214640.3875120.7079220.7514330.5148690.1767350.3651090.1355990.1755370.5401350.6448510.4644710.3936550.5564370.9378460.672210.7086240.6255870.5435860.3218940.04270260.7221660.392930.8008150.2856840.6129620.1645110.181280.7143380.3613060.2798030.05307020.7270380.2253440.1005640.0948020.08040830.8014120.7536750.1792630.6338910.09323950.8452530.902740.230410.6694980.3057420.03250720.0979940.110720.6877020.5542360.2050860.7528010.9405320.4576780.6001770.8954870.9384080.9291680.8138180.958920.1639210.2291380.5070920.7293090.17680.06028070.6908330.5953260.688710.2586280.653880.5782740.715230.2935440.8535460.800230.537450.5648350.26380.5379570.5351490.2474820.5881770.05664930.861760.9350220.7426060.1251890.862635
990.2888210.5055640.5080480.5064010.7215520.8843280.352380.8366660.8558010.2008880.9904860.9451730.534070.1228030.4160340.5888270.05252210.03256830.8836580.3311590.7946340.7273240.4381930.9586780.1407090.9021560.5865090.4427830.9667640.144690.6845910.6151250.8892460.2361250.5986390.7155730.4053530.9124030.4372060.192910.3895970.08588370.3773540.27420.4212210.3703210.4624480.8300350.9621180.6951340.7350290.05779850.3785220.04835530.7324210.5756970.733580.3368160.2627850.5767060.7178420.8800580.7029490.9879470.1675510.5937980.603810.3085640.3688390.7229520.2807350.6971940.3072630.371120.3204380.5559650.5224990.6770060.04518520.4510280.3689860.4898820.222290.09037770.1899140.6089580.0483090.8822250.20730.9355150.6875340.9855560.880850.3345210.946510.309930.1236760.01153550.9578360.340879
1000.1956220.9174590.2225910.8041950.9334940.1338990.462860.3010810.5719430.7902670.09439370.6690830.7400540.6705780.9929630.2216480.01039680.1449370.4464730.4206220.6065280.8054530.6683770.2835920.7164120.8876450.8666820.5630040.2523610.1955830.4005290.4690260.7105930.8751050.7846860.1114120.3063760.7427070.3906630.3786470.8411030.9372040.07569130.3751980.3750460.6609630.5589640.7797880.3946160.9554270.4874250.5784760.9671210.3614180.9014430.4788250.5284920.8005450.3656180.5474290.5807170.319030.3603690.9248350.07897240.4339440.8732680.02208150.01759440.2654790.8361220.4392790.5549180.6340130.4404580.1184610.9912610.6420730.9768780.163160.6655390.9671130.4297870.4652640.9782240.2534360.9084260.09134280.009131310.209440.2636050.2584180.2566140.2228240.7236050.932640.008630390.657740.6880510.499143

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       x19       x20 ⋯
     │ Float64   Float64    Float64   Float64   Float64   Float64   Float64   Float64   Float64   Float64  Float64   Float64   Float64  Float64   Float64   Float64   Float64  Float64   Float64   Flo ⋯
─────┼──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
   1 │ 0.284257  0.0205724  0.823952  0.428349  0.495671  0.936988  0.211265  0.317921  0.287244  0.48268  0.922129  0.228133  0.35726  0.903001  0.683951  0.578339  0.63342  0.229544  0.401198  0.5 ⋯
  ⋮  │    ⋮          ⋮         ⋮         ⋮         ⋮         ⋮         ⋮         ⋮         ⋮         ⋮        ⋮         ⋮         ⋮        ⋮         ⋮         ⋮         ⋮        ⋮         ⋮          ⋱
                                                                                                                                                                          81 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.6301450.1740970.1014470.7303090.0588846
20.5307320.1391260.4776220.9238490.0537085
30.7237520.2754890.324030.3316160.326954
40.2829430.7027430.4049420.9251550.887331
x[:, Between(:x2, :x4)]
4×3 DataFrame
Rowx2x3x4
Float64Float64Float64
10.1740970.1014470.730309
20.1391260.4776220.923849
30.2754890.324030.331616
40.7027430.4049420.925155
x[:, Cols("x1", Between("x2", "x4"))]
4×4 DataFrame
Rowx1x2x3x4
Float64Float64Float64Float64
10.6301450.1740970.1014470.730309
20.5307320.1391260.4776220.923849
30.7237520.2754890.324030.331616
40.2829430.7027430.4049420.925155

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

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

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

a SubDataFrame

@view x[1:2, 1:2]
2×2 SubDataFrame
Rowx1x2
Float64Float64
10.6301450.174097
20.5307320.139126

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.3900470.8846120.00446181
20.2670210.1318930.935558
df2 = copy(df)
2×3 DataFrame
Rowx1x2x3
Float64Float64Float64
10.3900470.8846120.00446181
20.2670210.1318930.935558

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.3900470.8846120.00446181
20.2670210.1318930.935558
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.