IngridM IngridM - 11 months ago 49
Python Question

How to use For loop KEY to name DataFrame in Pandas

I want to use the strings listed in List_ and pass them in my function lambda so that each iteration names changes for load or lambda x: x.load.reset_index().T

List_ = {'load','Temp','Hum','Hum'}
list__ = []
for names in List_:
test = df.apply(lambda x: x.names.reset_index().T)


df4 is a dataframe with Date as index and columns with load, temp , hum and windspeed.

Date  Load    Temp
1     100     1.1
1     200     1.2
1     300     2.4
2     400     1.7
2     500     4.3
3     600     2.2  

What I wanted to do was using a loop for to apply my function to each columns within the group and create new transposed dataframes using the KEY basically the name of my columns.

grouped = df4.groupby('Date')
cols = {'load','Temp','Windspeed','Hum'}
df_dict = {}
for col in cols:
     df_dict[col] = grouped[col].apply(lambda x: x.reset_index().T)

Anyways, I have learnt two things during that painful experience - For loops use KEY and the use of in progress