2015-11-20 21 views
2

我需要一個.csv文件的內容從轉換:轉換多個值列到新的鍵,值對

continent, region, country, 1990, 1991, 1992, 1993, ... 
Africa, East Africa, Ethiopia, 12, 14, 15, 9, ... 

到:

continent, region, country, year, value 
Africa, East Africa, Ethiopia, 1990, 12 
Africa, East Africa, Ethiopia, 1991, 14 
Africa, East Africa, Ethiopia, 1992, 15 
Africa, East Africa, Ethiopia, 1993, 9 
... 

還有一堆行的,所以這是不可能手工完成。我知道必須有一種方式來做到這一點,我真的只是有問題甚至解釋我在做什麼。它基本上是轉換桌子的一部分,但不是全部。

編輯:這是一個完整的數據行的示例:

continent, region, country, country_abbr, economy, lat, long, 1990/91, 1991/92, 1992/93, 1993/94, 1994/95, 1995/96, 1996/97, 1997/98, 1998/99, 1999/00, 2000/01, 2001/02, 2002/03, 2003/04, 2004/05, 2005/06, 2006/07, 2007/08, 2008/09, 2009/10, 2010/11, 2011/12, 2012/13, 2013/14, 2014/15 
Africa, Middle Africa, Angola, AO, 7. Least developed region, -12.5, 18.5, 50, 79, 78, 33, 77, 62, 71, 64, 85, 55, 50, 21, 57, 38, 15, 25, 35, 36, 38, 13, 35, 29, 33, 35, 39 
+0

'year'的值當前是文件中的標題,'value'位於其下方的列中。這些話在技術上還不存在,我只需要對數據進行不同的編碼。存在於我現有表格中的每一行都將變成25個新行(這是我擁有多少年)。 –

回答

1

不太清楚你的意思這些話在技術上並不只是存在之前尚未所以我提出country列第一年專欄,並且目前忽略country左側的任何內容。 'Unpivot',詳細描述如下here,並且選擇country列到最後一列,並且只填充許多行。

生成的表應該是三列,每個國家有一組25行。第一列(「行」 - 國名)可能用於查找我上面忽略的細節。