I'm relatively new to python, but have been working on a pet project looking at housing markets. Effectively I have a series of files that I've read in and stored as variables with the raw format as follows:
**
cityrentalprice1BR.columns
Index([u'RegionName', u'State', u'Metro', u'CountyName', u'SizeRank',
u'2010-02', u'2010-03', u'2010-04', u'2010-05', u'2010-06',
...
u'2017-09', u'2017-10', u'2017-11', u'2017-12', u'2018-01', u'2018-02',
u'2018-03', u'2018-04', u'2018-05', u'2018-06'],
dtype='object', length=106)**
The "wide" formatting is not particularly helpful for my exercise so I've used the melt function to have a better timeseries by city and the results are great on single dataframe:
cityrentalprice1BR_melt = pd.melt(cityrentalprice1BR, id_vars=['RegionName', 'State', 'Metro', 'CountyName', 'SizeRank'],
value_vars = dates,
value_name = 'Value',
var_name =['Date'])
cityrentalprice1BR_melt['Date'] = pd.to_datetime(cityrentalprice1BR_melt['Date'], format='%Y-%m')
My problem arises in that I have multiple files to import with the same formatting and I'd prefer to not manually melt each dataframe. One big caveat is I'd prefer to have the results pushed into new unique dataframes (i.e. in the example above cityrentalprice1BR > cityrentalprice1BR_melt).
I've been working on this for a while and have created a list of dataframes and can apply the melt function to the entire list as follows, however it is missing my key goal of retaining separate dataframes (or column sets per dataframe if a merge function is more appropriate):
Rental = [cityrentalprice1BR, cityrentalprice2BR, cityrentalprice3BR, cityrentalprice4BR, cityrentalprice5BR, cityrentalpriceCondo, cityrentalpriceDupTri]
for size in Rental:
transformrent = pd.melt(size, id_vars=['RegionName', 'State', 'Metro', 'CountyName', 'SizeRank'],
value_vars = dates,
value_name = 'Value',
var_name =['Date'])
Any guidance would be much appreciated.