Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Beginner problem with pandas and data frame

In this code, there is the part with a lambda fucntion

symbol = ["XOM", "BP", "COP", "TOT"]  
start = "2012-01-01"  
end = "2016-01-01"  
prices = get_pricing(symbol, start_date=start, end_date=end, fields="price")  
if isinstance(symbol, list):  
    prices.columns = map(lambda x: x.symbol, prices.columns)  
else:  
    prices.name = symbol  

can anyone please explain what is happening in prices.columns = map(lambda x: x.symbol, prices.columns) thanks

1 response

It's just a way to replace a complicated set of column labels such as Equity(88347[XOM]) with simpler labels XOM. In this case XOM is the symbol attribute of equity object Equity(88347[XOM]).

The python function map is not very intuitive. An easier (and more intuitive) way to do this would be using list comprehension.

prices.columns = (x.symbol for x in prices.columns)