The Pandas Index function dropna() used to remove the empty (na/nan) elements from the Index object. It returns an Index object without empty values.
1 Index.dropna(how = 'any')
how : It is string that specifies how to remove an empty elements. The possible values are any, all. If not specified, the default value will be any.
None : If the Index is a MultiIndex, drop the value when any or all levels are NaN.
1 import pandas as pd
2 import numpy as np
3
4 index = pd.Index([92, np.nan, 23, 45, np.nan, 92])
5
6 print('The original Index object :')
7 print(index)
8
9 res = index.dropna()
10 print('The Index by dropping na :')
11 print(res)
In the above example, an Index object is created by passing an array that includes nan value elements. A dropna() function is called that removes any nan value elements from the resulting Index object. The result is assign to a variable that will be printed on console.
1 The original Index object :
2 Index([92.0, nan, 23.0, 45.0, nan, 92.0], dtype='float64')
3
4 The Index by dropping na :
5 Index([92.0, 23.0, 45.0, 92.0], dtype='float64')
Index with string literals and numbers
Index with string literals and numbers
1 import pandas as pd
2 import numpy as np
3
4 index = pd.Index([1, 2, 'Welcome', np.nan, 'IOGayan'])
5
6 print('The original Index object :')
7 print(index)
8
9 res = index.dropna()
10 print('The Index by dropping na :')
11 print(res)
In the above example, an Index object is created by passing an array of string literals and numbers that includes nan value elements. A dropna() function is called that removes any nan value elements from the resulting Index object. The result is assign to a variable that will be print.
1 The original Index object :
2 Index([1, 2, 'Welcome', nan, 'IOGayan'], dtype='object')
3
4 The Index by dropping na :
5 Index([1, 2, 'Welcome', 'IOGayan'], dtype='object')
Index with string literals
Index with string literals
1 import pandas as pd
2 import numpy as np
3
4 index = pd.Index(['Welcome', np.nan, 'IOGayan'])
5
6 print('The original Index object :')
7 print(index)
8
9 res = index.dropna()
10 print('The Index by dropping na :')
11 print(res)
In the above example, an Index object is created by passing an array of string literals that includes nan value elements. A dropna() function is called that removes any nan value elements from the resulting Index object. The result is assign to a variable that will be print.
Index with string literals
1 The original Index object :
2 Index(['Welcome', nan, 'IOGayan'], dtype='object')
3
4 The Index by dropping na :
5 Index(['Welcome', 'IOGayan'], dtype='object')
Related options for your search