Python Forum

Full Version: kindly support with this dropna function not working
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello,

I am beginner in python. This is my code.

In line 12, i wrote a "dropna" function to avoid a row having a blank field in the column named "documented". but still the "nan: appearing while printing it. kindly help please.


file_name = "C:/Users/Gheevar/Documents/PYTHON CLASS/home study/PRACTICE WORK BOOK.xlsx"
sheet = "Sheet1"  ###Sheet name

import pandas as pd   ##pandas
df = pd.read_excel(io=file_name, sheet_name=sheet)

df['SPECIALITY'] = df['SPECIALITY'].str.lower()   ##converting specality to lower case
pd.set_option('display.max_columns', None)        ##to display all columns below
pd.set_option('display.max_rows', None)             ##to display all rows below

df.replace({"SPECIALITY":["pediatric trauma","trauma surgery","ortho_trauma"]}, "trauma")  ##replacing values

df.dropna(subset = ['DOCUMENTED'], inplace=True)   ##remove the line having blank value in documented column

print(df)



df.pivot(index= 'DOCUMENTED', columns='SPECIALITY', values="PT URN")  ## The pivot table also not appearing for me while am writing this code. kindly help this also if possible
The excel data what am using is attached here.
file_name = "C:/Users/Gheevar/Documents/PYTHON CLASS/home study/PRACTICE WORK BOOK.xlsx"
sheet = "Sheet1"  ###Sheet name

import pandas as pd   ##pandas
df = pd.read_excel(io=file_name, sheet_name=sheet)

df['SPECIALITY'] = df['SPECIALITY'].str.lower()   ##convering specality to lower case
pd.set_option('display.max_columns', None)        ##to display all columns below
pd.set_option('display.max_rows', None)

df.replace({"SPECIALITY":["pediatric trauma","trauma surgery","ortho_trauma"]}, "trauma")

df.dropna(subset = ['DOCUMENTED'], inplace=True)

df.pivot(index= 'DOCUMENTED', columns='SPECIALITY', values="PT URN")
I ran your code and the dropna worked fine for me. Print the table before and after the dropna and you will see the NaNs are gone. For help on the pivot table you need to provide more information. Other than not saving the results of the pivot, it also appears to work.
Output:
SPECIALITY general ortho_trauma pediatric trauma trauma surgery DOCUMENTED 0.0 5.535367e+09 44554564.0 6.546545e+09 NaN 1.0 3.113313e+06 NaN NaN 5454543.0
The NAN's here are valid as there are no undocumented (0.0) trama surgery or documented (1.0) ortho_trama or pediatric trama. What are you trying to do?