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
1935795721010
294456653101
3104632108716
41091541010878
5261067437103
6210710796462
721036569535
814581044456
91332988767
1043678741073
1152993991067
1258318336810
139451449516
98941816910672
990521010665477
991441033941013
99227891057215
9935287743164
994110544107141
9956961457235
99623341776410
9979561475578
9981046110139310
99962101849553
1000109341091182

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

first(y, 5)
5×10 DataFrame
Rowx1x2x3x4x5x6x7x8x9x10
Int64Int64Int64Int64Int64Int64Int64Int64Int64Int64
1935795721010
294456653101
3104632108716
41091541010878
5261067437103

and last to see its bottom rows.

last(y, 3)
3×10 DataFrame
Rowx1x2x3x4x5x6x7x8x9x10
Int64Int64Int64Int64Int64Int64Int64Int64Int64Int64
11046110139310
262101849553
3109341091182

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

first(y)
DataFrameRow (10 columns)
Rowx1x2x3x4x5x6x7x8x9x10
Int64Int64Int64Int64Int64Int64Int64Int64Int64Int64
1935795721010
last(y)
DataFrameRow (10 columns)
Rowx1x2x3x4x5x6x7x8x9x10
Int64Int64Int64Int64Int64Int64Int64Int64Int64Int64
1000109341091182

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.6152480.9121870.003296760.1157710.5533570.1641680.3654420.7030230.876210.1148950.6105830.05422110.943180.7549410.4835280.7032270.7400770.4243730.6648540.2787460.3251140.202090.9561340.3221840.4085230.01588390.1764250.4009980.8696060.3096950.9668120.8260780.7145460.5079940.762720.5941680.08269620.9831680.2837710.1949830.3083290.8334460.4865940.03603930.5446940.1700520.2140160.899320.8017320.1627220.7144590.03659350.5634660.3946560.6660950.8456850.5554380.6443210.3404860.2210720.4128670.2033020.246580.7405710.7102690.04049310.4141940.145340.3129270.6245830.1288710.01601480.5274290.3943340.006591910.02093920.00872640.05945750.2175730.9562260.9891440.9565590.1662360.1714440.02302780.4845240.1399530.6353460.3005450.9113280.3777940.7263840.1731710.2428220.5056430.8038960.7724250.3486070.9165860.455879
20.3308590.1618110.6564580.1502360.8503510.8999740.07966250.1267140.4153260.4722840.22770.0150170.8368290.6269850.02570460.5333580.3792980.8648330.3118710.1508320.6014990.3995210.5636690.9994810.1770960.9188150.7586350.4427740.1376560.6209810.03991570.2997870.8851960.0001548620.7114770.7045850.4238730.7043310.7713550.4296690.8936350.2810180.6312260.6283020.4309190.8606160.8660630.3007910.1466910.8157220.8746680.8747250.7518580.02349970.6866720.1160670.5402020.696040.8541430.7121940.1111020.5462860.3938410.6705110.7207920.4428220.4938430.09025450.3824950.2465040.3313770.7685050.04219170.6375080.5671670.9067580.1824110.2739030.1780710.08668090.4174110.053490.8364250.4640580.6609520.7433690.5808490.8848080.07655540.7217960.4139620.3076950.5857020.6618390.3170410.4809610.7775130.235860.9960380.903163
30.2467360.1010440.2252520.9377630.6561840.8446370.4869970.02194260.7920660.2488970.6260880.4761290.1645570.08637320.2521420.0776470.5637420.1205970.7088140.2672420.8512430.9360450.7337710.7636980.5159340.5126070.6396390.1953580.9578710.7415120.779680.4588390.9151490.8432640.2301910.9868160.115970.4486280.3113110.5598390.9824020.01174990.9083750.7182620.8400440.4418090.8466830.3194530.5080850.03064650.1398190.3412740.02212130.4967690.003539520.9182470.09526760.8615840.07780170.5842640.149410.9479860.1634720.8518660.5934410.6784380.9882220.6114640.4155520.8629010.408870.2896160.5606630.3214340.5868660.5714820.111720.867710.7952560.9692870.8027510.4069170.07675920.3045620.2088790.7605610.9114860.3351320.887190.7004440.1066380.4311380.5567090.3475920.8248510.1900140.5331690.2160630.4465370.425688
40.7694810.07736480.349560.8491680.5201270.108840.9366460.9602710.483830.9354190.2936360.2844770.608710.2386220.1246770.9362850.6178730.5875530.04978520.3680790.5835790.3040780.6603260.3249970.4754520.5457340.1841690.6600250.3144260.7217820.4962690.540190.8919790.3810750.4809970.4912250.4421080.571560.5944720.5433060.1784060.2391920.4564790.2444230.8043650.3904620.416070.5131480.3037560.8462010.8979290.6708520.7668180.1177950.9594230.981460.7559740.3494650.4366310.22040.4349280.08692340.79470.04685480.2798450.5208080.9962660.3262060.3269850.8270680.2186820.370190.1033970.8948290.4979530.526960.1868970.1333770.2326310.8469770.6794630.4791080.8400180.304620.2674910.6173610.05224580.8915680.7500210.8227390.4361070.1085780.6911960.9713310.08508820.1002880.5910740.9470830.1964520.94587
50.9917190.9655140.2025870.1792130.8821070.5006020.7417020.1501230.7595530.8334320.6414810.6840720.162740.4672380.6891550.7165470.5927450.3035990.3105120.2401640.3623580.6298140.9111860.01143950.5703380.539690.6060590.7352450.3149670.6839760.2416690.9474350.3579390.1094040.6457990.4585660.8665940.2743160.4151750.2739020.4844770.7824760.9106310.3179270.9957580.52960.1538520.4737090.9897840.005877870.1803640.5206230.7047520.5124480.2244980.9815040.6900980.2636620.8380710.3472360.5548640.0928030.705130.1192410.1828490.5414880.8790090.2306670.6058170.8214430.4100860.7807160.7378280.02639220.3825810.7266420.3040430.8007930.7037950.9728970.2515310.8574770.920180.1176080.3896390.4567820.6761790.1234240.2630990.9693630.03113070.8743540.9153490.1351970.3746820.4780160.5472070.628640.66990.0714617
60.5215340.08645130.4106830.01948240.4284610.6789970.7468470.002387610.1357680.327680.1165240.9813710.459280.2901560.9523830.09590980.9243480.7575940.2382490.10940.87040.2757870.9970050.4591420.6352150.4008880.927350.7961640.3043020.1054240.4709820.9038190.6063580.4237720.7921040.536940.7472020.7478420.2844280.5942030.3049460.9130810.5762040.9227410.9870770.657330.8301480.1413330.1104680.2333290.2263780.9184480.3210120.3227260.03473040.976220.5521740.08331550.05910320.5678690.07097510.241360.3900890.1534260.1935220.5364660.4589460.1917320.4919780.7380340.2023180.1769740.3493270.7525760.1169110.7569330.7222870.9727180.0349030.7315520.1903450.4805950.004305480.8600120.8068880.8050050.8817690.3687660.7541320.3847520.4133480.8060660.4731690.1219480.7802290.08380960.3342960.02209970.7377290.775145
70.6844610.7352960.6987730.09199190.2551660.4532880.4601140.2504390.654620.05426450.01272150.7633660.6104870.8881870.895710.3859370.7697960.3369650.5523220.5690580.1500960.9423130.8040280.2238260.7643240.1717640.07287410.1580550.3916460.3041620.8726960.01599690.4101560.8657730.3990480.5918140.7085120.4179150.6795690.9360150.4538790.4036680.7769180.7164540.1377160.2739780.2384070.8021060.3455540.3619230.6792230.4610840.4338810.4108140.1416450.6193690.8162670.5917080.2386320.8739830.4593870.8940740.3100360.6984610.7554780.5686270.8330070.7211340.5907260.9945210.6441330.6006840.4593460.7943070.4310290.7829020.7269360.9950480.6585780.7101190.8728870.5804390.7463050.1784860.82790.6745840.8239650.1449060.01789630.5509560.4967750.00270390.3873130.7416330.05058320.2586970.09264260.02144190.7855050.78137
80.5762480.6278910.9973170.009763580.04112830.2628780.3746360.9857670.8700110.2412440.8357380.2691650.9211820.6571240.2776860.8964680.4540350.280010.5890250.1639830.5362040.2000520.676460.7640460.3132780.7141460.2697040.9757890.5832460.43150.2757510.5745580.3963010.145790.9908110.7922830.3651830.9279590.3257770.1450250.2879620.1274790.7363530.3149610.6348080.6785950.02942370.8700950.5183190.7457320.4294040.5475470.3127190.4536610.8471140.2667530.8272650.7354620.9577660.6983340.6617230.463420.333690.2788430.1722670.7061360.298690.7212190.05756580.4272040.2712590.6911210.1212310.7081720.9796970.2637980.2947160.1407560.5606370.8126910.2572020.05755240.1019820.8332630.8260490.8551080.3276480.4415510.801330.1744080.3198610.08731460.7891080.8008530.1123970.2227230.9752750.8052940.03787820.0563243
90.7466060.712760.7417880.9378610.5299610.0777210.3537590.5236160.6581350.4423460.9440250.7324050.7043270.067490.9392790.2995330.1549060.03293330.9144490.1960.4759720.6891040.5205010.05479290.4247050.4093030.478340.6661120.08895680.4936810.1903780.9819520.2386860.4155380.5267090.1991530.1836390.3106260.1988260.4070450.662710.663180.7660910.7599470.8286390.0267440.7040310.7145810.3192280.6089950.6984960.9651220.8575320.1512650.349610.1921150.4024410.6533510.2171110.4063880.09171640.5164650.6211560.8032910.336550.27720.4713340.837520.1379390.3844070.9882050.354560.9731610.6483790.3853950.2228890.06131680.1969550.5184720.6703970.1783920.966930.6633160.6075320.0008296340.4424160.6366110.4317690.7833130.3794920.9901750.1585320.3499650.5074370.3677130.02041030.8740240.1700950.2781060.90374
100.2965560.9002040.8367820.964370.1052560.2488040.7859040.0429160.03300320.9570720.0162070.2008920.9691550.1389660.7206920.2258490.4678590.6461440.4540650.4897310.1644870.2125710.3309580.4936960.6896650.4830480.3754710.09173420.0533050.2193690.824630.512140.7794910.5104160.4421570.7604080.9172650.7965250.8528650.4366220.3144430.6417330.7327440.1407150.960910.6397810.1988060.02930920.7286770.3516090.5210740.2700280.1285150.3685160.8455010.6282050.4895850.1767460.3188280.02286080.9092110.4521950.5862980.2841690.04454570.03837480.8338460.4345850.2538360.4923750.8241860.2967430.3480260.3284010.08646550.5001650.8396830.2957350.4052170.7073740.5025170.285870.2984510.08043730.7474080.4465170.1381850.3899840.8398950.3169660.05548470.8495090.9814930.5006170.1391930.9457980.4804580.7059380.2998480.425466
110.5285130.8953080.1699810.5891230.6253010.3018320.4586720.2912180.7894930.7848030.04722110.3384720.6410040.2116380.982370.4207730.3057680.978460.3877020.08984860.408910.4016310.7429450.2095370.5839840.6470120.04718310.4282090.3495160.4217730.654320.6276340.8675920.01010330.3111580.4521460.6570170.2661150.6986540.5055990.6845170.7671350.8935420.2798020.5766460.4852970.6782130.4206590.598810.1622210.2373620.5490140.6477830.6837260.03048640.9143210.5929090.5481720.7882150.3590080.4062710.4672180.3230070.1116310.0968910.2298290.1682870.4616650.6131450.5435820.6282260.2970320.2649310.04557110.6478470.2452340.9931870.9769980.6200870.9386220.6280620.8014510.9119530.8195940.7755240.5418030.7489030.05843160.549760.08992710.6502010.4968520.7291760.3040860.3935030.6289450.144730.9587920.8304850.763519
120.2616220.9985960.4318310.9281810.3122270.5773630.440190.8947530.8888760.8980890.1929570.2622570.7239340.9049710.688410.716250.5850180.1015430.4257410.474250.1702530.4524220.1984660.2304950.557830.05675460.7194580.8674820.347260.1691690.7849440.06312960.1646940.427950.4094560.01728810.9087760.6453440.687770.6776290.1071380.2789590.6492340.8902990.5786460.957710.1161110.06922530.5262660.9568120.6293380.4576070.8557030.5983190.7410160.4552180.6127240.6513580.1308870.2178290.2511890.1056490.1812560.4091170.4438150.255860.125910.868970.7006770.8882870.2131070.8043410.8864330.07414590.2112420.2319710.6659050.09574320.13740.729020.07675480.03345030.9559260.3293080.8442540.4435080.3394520.9217550.4091950.168430.6882240.3430880.1865660.4338530.8216210.2205080.8353060.7203920.7795420.483657
130.3459090.5411740.596830.3359240.1879920.5250530.351310.1016050.8230810.5144440.2531580.8509820.2270380.7583470.4411630.5823070.002740270.8884770.7549860.7243650.3387220.9570480.9433860.7350240.9740030.1514830.1446350.6969790.3802720.9606030.4156160.7990540.5766520.5328020.4316790.9717310.2665410.8346340.7090360.9510020.4665310.5610830.1774840.9072860.9608560.1385170.3332690.0552060.06614650.4011280.6608420.4246320.5868980.05782930.4725970.15270.7510510.01683690.1382680.3706630.6891540.2911090.1227630.5289440.1686290.4117640.1664960.3932360.1624120.5213290.132880.8395070.237910.03362050.3398280.4975020.5848740.2087810.1416010.04021260.5807790.1533760.3245860.04752920.814680.4348020.8752640.1481350.4677350.6586760.7930320.5654830.1090710.7062160.620260.6521790.1889050.6745260.4074260.298996
890.03345550.4181330.7644620.1134370.9565550.81190.5275410.5100540.4113430.8100410.4440520.3693270.04383940.01220130.7982130.6858670.2257550.3924190.1259410.5377110.02200520.8853180.8709750.009569510.9992220.3057690.6891960.5744750.9545770.6917020.6338230.7583150.6653760.3325420.8280620.8718650.3768670.6923390.9257450.3503370.2384430.8320280.7185570.4491210.8933160.8610590.7545810.1690410.1842390.3695880.8302230.2288540.5833440.3123960.526270.9504290.7458890.6782390.6528990.7171440.2711620.5743760.5844450.8091310.8942990.525310.398090.5027260.7519650.160080.9425480.5937560.7421910.2780520.5267930.8397230.7685250.3341280.4751840.8954280.6921530.7138550.303470.1091680.01416650.04766910.6327950.8443860.3281360.7225250.8526650.3294510.7300810.4274460.4575470.6932930.2243990.1845690.04243560.0758434
900.02342190.3271980.2255410.4745230.5536930.6637840.8565930.5511930.03692990.3780320.9209970.4401320.3523690.6216960.3184930.4585940.9214360.7178090.6285610.8845220.730770.4425430.459320.07639550.1475980.0182640.1190570.4009440.07950640.8778190.03443120.3130260.7676260.5885840.9966360.1368330.4206730.08477660.75280.07056030.7876710.9180810.221110.8384630.8450040.08196720.3966130.1680740.05195790.1900760.703760.8832180.586750.8621050.0311310.4670010.2167670.8521920.8870220.1772650.658440.8357820.2763990.684880.4904950.4342860.5665060.9950120.7037340.9469760.7455970.7183550.0610380.001121110.4562310.860230.8574160.09464960.2515790.4341160.37650.3507340.8228050.7733250.9391870.02884180.3416980.193160.403040.0168850.631510.09857770.1205420.955260.3106670.3465220.4835350.7732930.4243690.471431
910.1296780.4823630.4699680.09500830.7886040.8387750.07059580.7586050.2027110.861590.3028340.8485810.1307860.3502360.0646570.3960120.3991790.5427980.8473060.7386370.2614410.9694880.1981680.2544520.4318290.688290.3973420.367520.07761980.4447070.639590.9816340.287390.7550870.3405370.2680240.7633710.2127430.2595630.3428380.2520370.6658280.6212230.09579030.5676370.5259460.7372640.09340280.8460930.6364410.7155280.4639440.4908820.7873480.7460750.8927480.04186090.5532230.08146810.8585820.111880.8508190.3058910.2920080.5327760.4881640.04602470.2610250.1817260.2223940.9502360.1652470.5099390.8152690.4782730.0212830.7768960.8625130.903030.7435080.1524030.118570.6591040.4784180.4164190.9622250.002287320.4778880.9177880.7092430.5066470.01264640.4238050.09223590.4369260.123840.9073440.2213110.1899930.0114313
920.4622090.2552220.8618020.4237580.9954180.9624160.323970.9768410.604230.4398330.3824220.1635010.8632540.8763260.9859030.07246950.3555750.06506040.9014160.6684770.2626970.1944940.424450.1500460.565660.3897120.3043320.06065090.2980540.03328050.3870530.5488090.3625160.7890890.337230.5445550.4910330.1966280.06803030.2620680.5530250.1542940.5308770.9729150.2712280.4406270.8824480.2735260.5742110.400910.3583310.1142560.5097350.02235920.6646980.9782060.01086880.7783550.5748130.178130.22530.608970.1801520.04171610.2679720.7465680.2335970.7632010.2566010.8972880.6615330.09681210.2029030.6818650.7757040.0002146820.2220920.3437470.09772850.4395680.7855640.1746940.8074230.9778160.08066610.500930.8985340.08960880.8601290.02876550.6107910.3581980.9997310.3067460.9910720.4729590.2522730.9318820.1985130.0768946
930.761370.2287340.6014060.224330.9101670.3312940.953280.4395540.7978350.5395570.07166740.5971740.6922860.983620.708350.3494430.1426390.5696610.1626580.7816060.4675860.2569280.8261030.9954350.6933860.9668740.4413420.07551930.6259810.8936950.7893890.5731620.3971950.5998450.173740.8407720.131860.2626610.0927570.315980.3292150.06789080.4277720.1780040.8558650.1058730.3913790.06866890.0009539150.855160.02459240.2167130.8173540.2173780.7769310.5219120.4338650.214150.3657760.5365240.998030.01643260.2044920.6501620.1338810.7226270.09270060.1761840.9409970.6971060.2333990.5016120.2477110.8967260.4386610.8768550.1307140.03835920.4624010.3268350.7521230.5302410.1703840.4990740.06879470.445890.1769610.5801040.7287120.1592650.198210.1596990.2696750.3776210.9413870.1104350.8653950.67440.3681650.93641
940.9738270.9398870.9886390.8940460.1524650.1423890.1413420.1254540.7293590.208860.6763060.8687670.02210890.05893380.7447830.9315840.9203830.5730250.2627760.2323990.635430.3181110.1704770.3848340.4164220.5959980.3888730.5768220.9243290.393130.5954860.4397620.0427220.07452890.5096280.1622080.421160.5299750.7781370.2802610.02550180.7225160.9227780.7021870.5275360.03238130.9774550.347390.539060.7564130.02621290.769150.03804140.2960170.4051460.5616640.9120860.4332320.2335360.5777210.9151430.9659950.9387020.8403960.5344590.8970090.7539340.8919430.5454420.240760.7729540.3802670.2686250.2034790.3772960.4535250.4131530.8173780.1739540.04357580.7308820.9088760.3373050.6871610.8565080.6163160.06017980.08430410.749120.3104430.360380.7496970.02266470.3573050.305010.7303440.3527520.8945220.6453940.330291
950.5380590.1589630.136620.06973260.4409240.3961290.9722180.6523990.2139790.6796250.923990.2811510.6802630.9142040.006899920.9164230.2058860.4081470.1051540.3039850.5544320.2398650.09563170.778640.6927440.03960320.6559390.401540.2962210.8266920.7824120.4246110.6223430.8145370.06826380.8113610.5051070.7662680.9199380.1866140.405270.1120390.02554220.9204570.2940960.4063150.639190.8317480.4146650.8279150.5896310.6264140.3312490.8763720.6349810.6997860.7806420.604560.2700960.8894960.04892560.8396260.7486180.4659220.763940.702470.266380.4617730.8877740.9001850.1255280.1113360.1808580.3133030.300630.6911770.2855380.7165820.1523850.7441160.2405020.2184650.2400180.4437730.6607530.9633390.5082750.6021160.7273040.1387020.1584230.7933920.9887980.01755180.3939960.01192910.1790250.3246460.6050180.885227
960.2043070.5146870.2429410.258150.6982810.1502870.481860.7056890.3707730.08266050.5471690.4023370.7138850.3812170.7784860.8959070.6713640.9969780.3546960.6651530.6548690.4285640.02140630.05302270.1336740.8338460.9025660.8918080.4605780.5230490.4273530.9039460.9948590.3081910.04674530.4553980.4405840.6214820.1368290.8414530.9658050.5023870.6060220.007062430.6936320.4505630.4615160.2609550.09059480.4256950.9370790.8250580.03002930.1750990.4483850.7194410.6368830.7040910.17740.3009410.6152840.3886860.5798640.8188360.9487570.3290130.8237170.7282670.07598870.8862560.5706440.3189430.5510040.5150450.3488990.2447830.2004060.6943370.7756240.2903660.5885070.2864730.6021980.5246750.4422420.3014550.7915970.01260380.4343820.6242980.4282420.4572060.2381990.3708740.8863360.8503230.1877250.4072290.6206120.659965
970.1073620.03341660.8693670.7386260.4450750.3914290.9293020.5213340.7658360.8520170.7871130.9346340.5006320.7259130.7701120.7987010.1188970.6792740.5756350.499130.04499960.1177910.2161890.8946160.6537040.7288090.2181920.4777380.3480360.0115960.008963870.5453880.2390630.4755760.07937740.2257230.7732140.3922450.308060.9354780.988610.1982740.8609380.2889160.0959960.2319230.04912870.007346510.1364270.6611420.636710.5628790.6848010.01386860.06369640.5008710.6691150.5935870.7236920.3801680.00264050.2689130.7283240.3887240.6652370.5964170.3076310.6795350.49190.8302460.326150.5800750.2026370.9599330.475210.02942440.9601720.2252930.8369220.9428480.7469020.9831570.7440420.5827870.44030.448220.05070150.1590530.9872940.2633360.631310.2427960.1379630.047350.6147740.458490.3102910.7918050.005037880.286875
980.2622620.2472460.7839630.9298080.1621020.1761760.2335860.1671940.7323210.5305920.7685250.5223980.1890120.8287480.699510.9558160.2721430.4922030.9505760.8843720.7520240.3888570.7464230.1839060.8748330.3358620.7582220.1392840.9882810.8291270.9014530.8978860.5281210.4776020.7724380.2890060.03791290.7232050.5601580.07185070.3162640.4883960.1128930.05231740.2267050.9811420.08034390.8649790.6302210.2338060.6389680.7419390.4556380.4472320.4407690.8791010.9245750.7011130.3324950.2129950.8204660.5242110.1937280.6849650.6031120.8719620.1709310.5095230.1338420.5436940.4475620.9835920.2948880.4587810.2248340.008961150.5774940.6991210.004729730.5160670.4804070.6742950.7946890.4780250.9773410.4852760.9860860.6555930.2061520.8322130.456930.6412760.3918960.8792230.1815650.1130670.03627470.0144470.3315940.481281
990.345190.9905840.4404960.7912350.3671490.7992920.2962520.4679220.2554950.708130.6631630.3601780.539210.4955420.03110780.3276850.597970.4612750.4612510.980920.4596770.1918620.356410.1088230.07656540.4320130.247580.5369970.7540720.786540.9954270.7860890.2341420.9973350.3832940.5236830.4612390.1090260.305690.6194010.7540160.2782560.708150.9963380.3870850.5871170.7556990.225960.4935210.6184550.7436620.000187780.7253790.3952790.2676070.5246580.3798610.5954660.161520.2047940.218670.3827530.6190730.5880810.5697150.3477190.6111890.4167620.08492930.7321770.8048510.7684630.7967240.06660470.520660.4610560.0007815140.6603880.7877890.3298450.1197440.612020.1233790.5868240.6026370.700760.6725160.6451490.7856490.5483070.9204760.3966470.1867420.1769780.7258280.6158570.3738260.8454080.3199920.619119
1000.08435790.4147850.0355460.1828650.2598060.5967220.09941750.9527990.7022430.4094960.9065760.9522130.9414560.9473690.1411260.7890910.8499280.3064110.3207050.2593220.04589650.4263620.7833530.5276070.4755840.6438780.6691440.1932280.6824490.7447030.2082020.5981830.5187850.4030470.1201210.9385130.6778450.2260650.9929970.4788440.9919870.8010240.0378630.1291710.6064580.1387160.7284350.3582960.8405310.2169980.6797610.3483480.02216080.6810370.849550.105190.7054770.7897270.07346880.4754070.9181450.7327390.6937790.7036160.2324930.6476440.717110.04855040.1267750.4932470.1412390.6528760.3308430.7755340.6402980.8469630.2495710.8480780.5663640.002184890.2323350.9333420.7508260.09384330.8570170.9976140.1536940.5194760.4798130.7744680.1472360.3856550.9018620.5209910.1027970.5026440.15930.9514260.9107170.509851

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        ⋯
     │ Float64   Float64   Float64     Float64   Float64   Float64   Float64   Float64   Float64  Float64   Float64   Float64    Float64  Float64   Float64   Float64   Float64   Float64   Float64    ⋯
─────┼──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
   1 │ 0.615248  0.912187  0.00329676  0.115771  0.553357  0.164168  0.365442  0.703023  0.87621  0.114895  0.610583  0.0542211  0.94318  0.754941  0.483528  0.703227  0.740077  0.424373  0.664854   ⋯
  ⋮  │    ⋮         ⋮          ⋮          ⋮         ⋮         ⋮         ⋮         ⋮         ⋮        ⋮         ⋮          ⋮         ⋮        ⋮         ⋮         ⋮         ⋮         ⋮         ⋮       ⋱
                                                                                                                                                                          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.4850210.7680220.7101490.6946620.321754
20.9686870.3290020.8650640.1473070.316669
30.8711880.1669620.2746290.9921380.698987
40.7175580.2419120.6706860.2775230.62672
x[:, Between(:x2, :x4)]
4×3 DataFrame
Rowx2x3x4
Float64Float64Float64
10.7680220.7101490.694662
20.3290020.8650640.147307
30.1669620.2746290.992138
40.2419120.6706860.277523
x[:, Cols("x1", Between("x2", "x4"))]
4×4 DataFrame
Rowx1x2x3x4
Float64Float64Float64Float64
10.4850210.7680220.7101490.694662
20.9686870.3290020.8650640.147307
30.8711880.1669620.2746290.992138
40.7175580.2419120.6706860.277523

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

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

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

a SubDataFrame

@view x[1:2, 1:2]
2×2 SubDataFrame
Rowx1x2
Float64Float64
10.4850210.768022
20.9686870.329002

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.6606440.6580550.349094
20.04677690.4634440.474337
df2 = copy(df)
2×3 DataFrame
Rowx1x2x3
Float64Float64Float64
10.6606440.6580550.349094
20.04677690.4634440.474337

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.6606440.6580550.349094
20.04677690.4634440.474337
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.