@@ -270,15 +270,15 @@ def wide_to_long(df, stubnames, i, j, sep="", suffix=r'\d+'):
270270 ... 'ht2': [3.4, 3.8, 2.9, 3.2, 2.8, 2.4, 3.3, 3.4, 2.9]
271271 ... })
272272 >>> df
273- birth famid ht1 ht2
273+ famid birth ht1 ht2
274274 0 1 1 2.8 3.4
275- 1 2 1 2.9 3.8
276- 2 3 1 2.2 2.9
277- 3 1 2 2.0 3.2
275+ 1 1 2 2.9 3.8
276+ 2 1 3 2.2 2.9
277+ 3 2 1 2.0 3.2
278278 4 2 2 1.8 2.8
279- 5 3 2 1.9 2.4
280- 6 1 3 2.2 3.3
281- 7 2 3 2.3 3.4
279+ 5 2 3 1.9 2.4
280+ 6 3 1 2.2 3.3
281+ 7 3 2 2.3 3.4
282282 8 3 3 2.1 2.9
283283 >>> l = pd.wide_to_long(df, stubnames='ht', i=['famid', 'birth'], j='age')
284284 >>> l
@@ -323,33 +323,29 @@ def wide_to_long(df, stubnames, i, j, sep="", suffix=r'\d+'):
323323 Less wieldy column names are also handled
324324
325325 >>> np.random.seed(0)
326- >>> df = pd.DataFrame({'A(quarterly )-2010': np.random.rand(3),
327- ... 'A(quarterly )-2011': np.random.rand(3),
328- ... 'B(quarterly )-2010': np.random.rand(3),
329- ... 'B(quarterly )-2011': np.random.rand(3),
326+ >>> df = pd.DataFrame({'A(weekly )-2010': np.random.rand(3),
327+ ... 'A(weekly )-2011': np.random.rand(3),
328+ ... 'B(weekly )-2010': np.random.rand(3),
329+ ... 'B(weekly )-2011': np.random.rand(3),
330330 ... 'X' : np.random.randint(3, size=3)})
331331 >>> df['id'] = df.index
332332 >>> df # doctest: +NORMALIZE_WHITESPACE, +ELLIPSIS
333- A(quarterly)-2010 A(quarterly)-2011 B(quarterly)-2010 ...
334- 0 0.548814 0.544883 0.437587 ...
335- 1 0.715189 0.423655 0.891773 ...
336- 2 0.602763 0.645894 0.963663 ...
337- X id
338- 0 0 0
339- 1 1 1
340- 2 1 2
341-
342- >>> pd.wide_to_long(df, ['A(quarterly)', 'B(quarterly)'], i='id',
333+ A(weekly)-2010 A(weekly)-2011 B(weekly)-2010 B(weekly)-2011 X id
334+ 0 0.548814 0.544883 0.437587 0.383442 0 0
335+ 1 0.715189 0.423655 0.891773 0.791725 1 1
336+ 2 0.602763 0.645894 0.963663 0.528895 1 2
337+
338+ >>> pd.wide_to_long(df, ['A(weekly)', 'B(weekly)'], i='id',
343339 ... j='year', sep='-')
344340 ... # doctest: +NORMALIZE_WHITESPACE
345- X A(quarterly ) B(quarterly )
341+ X A(weekly ) B(weekly )
346342 id year
347- 0 2010 0 0.548814 0.437587
348- 1 2010 1 0.715189 0.891773
349- 2 2010 1 0.602763 0.963663
350- 0 2011 0 0.544883 0.383442
351- 1 2011 1 0.423655 0.791725
352- 2 2011 1 0.645894 0.528895
343+ 0 2010 0 0.548814 0.437587
344+ 1 2010 1 0.715189 0.891773
345+ 2 2010 1 0.602763 0.963663
346+ 0 2011 0 0.544883 0.383442
347+ 1 2011 1 0.423655 0.791725
348+ 2 2011 1 0.645894 0.528895
353349
354350 If we have many columns, we could also use a regex to find our
355351 stubnames and pass that list on to wide_to_long
@@ -359,7 +355,7 @@ def wide_to_long(df, stubnames, i, j, sep="", suffix=r'\d+'):
359355 ... r'[A-B]\(.*\)').values if match != [] ])
360356 ... )
361357 >>> list(stubnames)
362- ['A(quarterly )', 'B(quarterly )']
358+ ['A(weekly )', 'B(weekly )']
363359
364360 All of the above examples have integers as suffixes. It is possible to
365361 have non-integers as suffixes.
@@ -371,19 +367,19 @@ def wide_to_long(df, stubnames, i, j, sep="", suffix=r'\d+'):
371367 ... 'ht_two': [3.4, 3.8, 2.9, 3.2, 2.8, 2.4, 3.3, 3.4, 2.9]
372368 ... })
373369 >>> df
374- birth famid ht_one ht_two
370+ famid birth ht_one ht_two
375371 0 1 1 2.8 3.4
376- 1 2 1 2.9 3.8
377- 2 3 1 2.2 2.9
378- 3 1 2 2.0 3.2
372+ 1 1 2 2.9 3.8
373+ 2 1 3 2.2 2.9
374+ 3 2 1 2.0 3.2
379375 4 2 2 1.8 2.8
380- 5 3 2 1.9 2.4
381- 6 1 3 2.2 3.3
382- 7 2 3 2.3 3.4
376+ 5 2 3 1.9 2.4
377+ 6 3 1 2.2 3.3
378+ 7 3 2 2.3 3.4
383379 8 3 3 2.1 2.9
384380
385381 >>> l = pd.wide_to_long(df, stubnames='ht', i=['famid', 'birth'], j='age',
386- sep='_', suffix='\w')
382+ ... sep='_', suffix='\w+ ')
387383 >>> l
388384 ... # doctest: +NORMALIZE_WHITESPACE
389385 ht
0 commit comments