I want to convert a Pandas DataFrame series to a List.

In [63]: bayFails
Out[64]:
0    [0, 1, 4, 5, 6, 8, 9, 12, 13, 14]
In [63]: type(bayFails)
Out[64]:
<class 'pandas.core.series.Series'>

Can someone show me how to convert bayFails into a list?

Recommended Answers

All 5 Replies

It appears that bayFails is a class instance

<class 'pandas.core.series.Series'>

and so can not be converted to a list. You will have to access the data within the class. Since we have no idea were bayFails comes from, the only advice would be to read the Pandas docs since extracting data would be rountinely done by many programmers (I would guess by using itertuples or iteritems).

This is my dataFrame.

In [63]: pandasDF
Out [63]: 
          LogBlk    Page                            BayFail 
    0          0       0  [0, 1, 4, 5, 6, 8, 9, 12, 13, 14]
    1          0      16  [0, 1, 4, 5, 6, 8, 9, 12, 13, 14]
    2          0      32  [0, 1, 4, 5, 6, 8, 9, 12, 13, 14]
    3          0      48  [0, 1, 4, 5, 6, 8, 9, 12, 13, 14]
    4          0      64  [0, 1, 4, 5, 6, 8, 9, 12, 13, 14]
    5          0      80  [0, 1, 4, 5, 6, 8, 9, 12, 13, 14]
    6          0      96  [0, 1, 4, 5, 6, 8, 9, 12, 13, 14]
    7          0     112  [0, 1, 4, 5, 6, 8, 9, 12, 13, 14]
    8          0     128  [0, 2, 6, 8, 9, 15]
    9          0     144  [0, 1, 4, 5, 6, 8, 9, 12, 13, 14]
    10         0     160  [0, 1, 4, 5, 6, 8, 9, 12, 13, 14]
    11         0     176  [0, 1, 4, 5, 6, 8, 9, 12, 13, 14]
    12         0     192  [0, 1, 4, 5, 6, 8, 9, 12, 13, 14]
    13         0     208  [0, 1, 4, 5, 6, 8, 9, 12, 13, 14]
    14         0     224  [0, 1, 4, 5, 6, 8, 9, 12, 13, 14]
    15         0     240  [0, 1, 4, 5, 6, 8, 9, 12, 13, 14]

My dataframe is pandasDF and it gives me the output above. What I want to do is to get the BayFail column for a specific LogBlk and Page. For instance, this is what I did to get the bay fails from Page=128 and Block = 0

In [63]: 
page = 128
block = 0
pandas1 = pandasDF[pandasDF['Page'] == page]
pandas2 = pandas1[pandas1['LogBlk']== block]
bayFails = pandas2['BayFail'

Out [63]:

[0, 2, 6, 8, 9, 15]

Now, I just want to be able to invert the output list to have items that are not in the list from 0 to 16. So my desired output would be

[1,3,4,5,7,10,11,12,13,14]

Is there any easy way to do this?

What your request seems to boil to is

page = 128
block = 0
print sorted(set(range(16)) - 
             set(pandasDF[pandasDF['Page'] == page]['LogBlk'] == block]['BayFail'])) 

But I have no means to check it.

Stupid question. But what's the easiest way to do these lines of code all on one line?

page = 128
block = 0
pandas1 = pandasDF[pandasDF['Page'] == page]
pandas2 = pandas1[pandas1['LogBlk']== block]
bayFails = pandas2['BayFail'to do this? 

This doesn't work for me for some reason.

pandasDF[pandasDF['Page'] == page][ ['LogBlk'] == block]['BayFail']

Argh, what am I doing wrong? I tried looking pandas documentation and it mentioned using ix but wanted to understand why my above code didn't work.

Pandas has a very good mailing list, perhaps you should consider joining it and posting this question there.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.