Cannot make sense of dataframe format returned by Python datastream API PyDSWS

Hello,

I use the simple python line of code to get the constituents of S&P500:

data1= ds.get_data(tickers="LS&PCOMP", fields='P,PE,MV,NAME,RIC')

However I cannot make sense of the dataframe it returns. It seems to have multiple layers and I can't see how to iterate through the dataframe to create a simple list of (Id, P,PE,MV,NAME,RIC) for each constituent of S&P500.

data1
Out[114]:
Instrument 891399 916328 545101 777953 906187 936365 749382 891631 \
Field P P P P P P P P
Date
2019-03-22 1764.77 77.97 18.06 323.21 139.45 26.37 259.6899 141.72

I am sure it's not that difficult but I would greatly appreciate help on this.

Many thanks.

Charles

Best Answer

  • I found a solution.

    The dataframe returned has a multi index. I wish the documentation on the community dev website would clearly mention that.

    Here is what I use to get the data for each constituent:

    	df_index = data1.transpose()
    for item in df_index.index.get_level_values(0):
    print(item)
    print(df_index.xs(item))