I have a df that looks like this
country country_abbr region year v1 v2 v3 ... v100
India IN IND 1999 327.0 7058.0 8856.0 ... 7900.0
Saint Lucia LC LCA 1990 NaN NaN NaN ... NaN
Samoa WS WSM 1985 NaN NaN NaN ... NaN
Somalia SO SOM 1999 136.0 643.0 678.0 ... 175.0
I'm looking for a simple way to collapse all of the vi
variables into a single variable v
(transform the table from wide to long) and then create a new column to hold the values of vi
by country
and v
.
I've tried stuff like this:
col_vals = df.columns.difference(['country', 'country_abbr', 'region', 'year'])
df2 = melt(df, col_vals, 'v', 'count')
df2['count'].astype(int)
But I'm noticing a lot of my data dropping out and I'm not sure why.
Edit to add expected output:
country year v count
India 1999 v1 327.0
India 1999 v2 7058.0
India 1999 v3 8856.0
Somalia 1999 v1 136.0
Somalia 1999 v2 643.0
Somalia 1999 v3 678.0