Pandas: Drop row if second column is blank

I am trying to use Python3 and Pandas to shape a dataframe.

My current frame looks like this:

<style type="text/css">
.tg  {border-collapse:collapse;border-spacing:0;}
.tg td{font-family:Arial, sans-serif;font-size:14px;padding:10px 5px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;}
.tg th{font-family:Arial, sans-serif;font-size:14px;font-weight:normal;padding:10px 5px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;}
.tg .tg-baqh{text-align:center;vertical-align:top}
<table class="tg">
    <th class="tg-baqh"></th>
    <th class="tg-baqh">Col1</th>
    <th class="tg-baqh">Col2</th>
    <th class="tg-baqh">Col3</th>
    <td class="tg-baqh">0</td>
    <td class="tg-baqh">X</td>
    <td class="tg-baqh">Y</td>
    <td class="tg-baqh"></td>
    <td class="tg-baqh">1</td>
    <td class="tg-baqh"></td>
    <td class="tg-baqh">Z</td>
    <td class="tg-baqh">B</td>

I am trying to drop the row if the second column is blank (so drop row 2, index 1, only here), but the name of the second column can change depending on the file being used so Im attempting to use ix but to no avail...

c = df.ix[:,1]
df.dropna(subset=c, how='all', inplace=True)

Any advice?

2 answers

  • answered 2018-03-20 18:25 Lior T

    I think this answers your question. You can get the columns names and store it to a variable with:

    x= df.columns.tolist()[0]



  • answered 2018-03-20 18:25 Estilus

    second = df[df.columns.tolist()[1]]
    df = df[second.notnull()]