I am a beginner to Python lambda. And try to convert the Python for loop to lambda expression. First I would like to explain the for loop lines.
fred = Fred2Hdfs() # construct the python imported objects
for i, state in enumerate(us_states):
df_unemployee_annual = fred.getFredDF('A', state, 'search_text') # generate dataframe from the object
if df_unemployee_annual is None:
continue
if i == 0:
fred.writeCsv2Hdfs('unemployee_annual.csv', df_unemployee_annual) # write dataframe
else:
fred.appendCsv2Hdfs('unemployee_annual.csv', df_unemployee_annual) # append dataframe
The above code work successfully without errors. And below codes are the Python lambda codes which I try to convert.
fred = Fred2Hdfs()
freq='A'
str='search_text'
result_df_list = list(map(lambda state: fred.getFredDF(freq, state, str), us_states))
result_df_list = list(filter(lambda df: df is not None, result_df_list))
print(result_df_list) # codes work correctly until this line.
#func=map(lambda df:fred.writeCsv2Hdfs('unemployee_annual_.csv', df) , result_df_list)
I am stuck with if i==0:
line in the for loop. How can I make the appropriate Python lambda expression from if i==0:
line. I am afraid I have no idea how to implement the if filter of Python lambda.
map(lambda (i,df):fred.writeCsv2Hdfs('unemployee_annual_.csv', df) if i == 0 else fred.appendCsv2Hdfs('unemployee_annual_.csv', df) , enumerate(result_df_list))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.