Caledonian26
Caledonian26

Reputation: 809

Removing NaN values from Pandas series - no prior post answers have worked

I have the following command which returns a Pandas Series as its output:

def run_ttest():
    for key,value in enumerate(data['RegionName']):
                 if value in stateslist:
                    indexing = data['differ'].iloc[key]
                    Townames.append(indexing) 
                 else:
                    indexing = data['differ'].iloc[key]
                    Notowns.append(indexing)
    Unitowns['Unitownvalues'] = Townames      
    Notunitowns['Notunitownvalues'] = Notowns 
    Notunitowns['Notunitownvalues'] = Notunitowns['Notunitownvalues']
    Unitowns['Unitownvalues'] = Unitowns['Unitownvalues']
    return Unitowns['Unitownvalues']
run_ttest() 

The output prints the series Unitowns['Unitownvalues']:

0     -32000.000000
1     -16200.000000
2     -12466.666667
3     -14600.000000
4        633.333333
5     -10600.000000
6      -6466.666667
7        800.000000
8      -3066.666667
9               NaN
10      1566.666667
11     10633.333333
12      6466.666667
13      1333.333333
14    -15233.333333
15    -11833.333333
16     -3200.000000
17     -1566.666667
18     -8333.333333
19      5166.666667
20      5033.333333
21     -6166.666667
22    -16366.666667
23    -22266.666667
24   -112766.666667
25      2566.666667
26      3000.000000
27     -5666.666667
28              NaN
Name: Unitownvalues, dtype: float64

I have tried the following:

     Notunitowns['Notunitownvalues'] = Notunitowns['Notunitownvalues'].s[~s.isnull()]
        Unitowns['Unitownvalues'] = Unitowns['Unitownvalues'].s[~s.isnull()]
     Notunitowns['Notunitownvalues'] = Notunitowns['Notunitownvalues'].dropna()
 Unitowns['Unitownvalues'] = Unitowns['Unitownvalues'].dropna()

But neither of these attempts have been successful.

There was a prior suggestion on a previous post referring to the conversion of the datatype to 'float', but since the type already is 'float64', adding .astype(float) does not solve the issue.

Would anybody be willing to give me a helping hand?

Upvotes: 0

Views: 60

Answers (1)

Bertil Johannes Ipsen
Bertil Johannes Ipsen

Reputation: 1766

Unitowns is a dataframe? In that case, I would do:

Unitowns.dropna(subset=['Unitownvalues'])

This wil get you a dataframe with rows dropped where Unitownvalues is na. If you just want the Series, Unitowns['Unitownvalues'].dropna() will work, but you can't assign it right back to the dataframe, as that column will not match the length of the other columns I assume you have (I guess this is the Error you are having).

Edit: Does the following not work for you? If not, what is your error?

s = run_ttest()
s = s.dropna()
s

Upvotes: 1

Related Questions