0

I have a CSV file that contains years in columns like this: enter image description here

I want to create one "year" column with the values in a new column.

I tried using pandas.melt, but it doesn't seem to be changing the dataframe. Here is the relevant code:

international_df = pd.read_csv("data/International/PASSENGER_DATA.csv",delimiter=',')
international_df.melt(id_vars=['Country Name','Country Code','Indicator Name','Indicator Code'],var_name='year',value_name='Passengers').sort_values('Country Name')

I have tried adding the years to a list and passing that in to value_vars, but this doesn't work either. If value_vars is not specified (as above), it should pivot on all columns that aren't in id_vars. Any idea why this isn't working?

AC1
  • 63
  • 1
  • 2
  • 7

1 Answers1

0

The .melt() function doesn't actually update the dataframe. Needed to save the returned frame:

international_df = pd.read_csv("data/International/PASSENGER_DATA.csv",delimiter=',')
print(international_df)
newdf = international_df.melt(id_vars=['Country Name','Country Code','Indicator Name','Indicator Code'],v
AC1
  • 63
  • 1
  • 2
  • 7