-5

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 temp....ie. 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)
    list__.append(df)
IngridM
  • 33
  • 1
  • 7
  • What's with the variable names? `List_` is a set! – jonrsharpe Jul 19 '16 at 15:23
  • why don't you just pass a list of the cols of interest? `test = df[['load','Temp','Hum','Hum']].apply(lambda x: x.reset_index().T)`? Actually won't `test = df[['load','Temp','Hum','Hum']].reset_index().T` just work? Also what are you trying to do here? – EdChum Jul 19 '16 at 15:24
  • The list you have defined is not actually a list, but a set. See `type(List_)`. Could you provide an example of your data? Also, you need to fix your indentation. – DrTRD Jul 19 '16 at 15:25
  • 3
    I honestly made some effort to see through your mistakes and understand what you were trying to do. I failed! You need to do better explaining yourself. Read these posts/articles http://stackoverflow.com/help/mcve and http://stackoverflow.com/help/how-to-ask – piRSquared Jul 19 '16 at 15:28
  • it won't work because I need the resulting df to be separated one from another. Ideally, I would have 4 resulting df which would be called df+names (but I don't know how to do that either) and I would apply my function to each column which are listed in the set (if you want, sorry I am new) separately and get 4 dataframes – IngridM Jul 19 '16 at 15:34
  • It is not that complicated to understand, each iteration I want names to be one of the listed ie. (lambda x : x.load.reset_index().T), that is all I am asking. – IngridM Jul 19 '16 at 15:38
  • Please provide sample input and expected output. I agree with the other comments that it's unclear what you're trying to do, and having sample input and expected output would help clear things up. See: [How to make good reproducible pandas examples](http://stackoverflow.com/a/20159305/3339965) – root Jul 19 '16 at 15:56
  • People, you don't need to be arrogant, that does not help at all!! It is not because you have been coding for year that everyone has. DrTRD in particular. Ridicule!! – IngridM Jul 19 '16 at 15:58
  • It is not about input or output, as I have already tested the lambda function which works perfectly and I have the result that I want, but because I have to do it several times, I just wanted to use a loop. Anyway... – IngridM Jul 19 '16 at 16:13

2 Answers2

0

When i understand this in the right way, this could be want you want:

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

When you change the last line you will get what you want. But i'm not sure because i don't know where "df" come from and what it is.

When this is a incorrect answer please show a bit more code so everybody could understand it better.

Melody
  • 182
  • 2
  • 12
0

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 dictionary...work in progress

IngridM
  • 33
  • 1
  • 7