Python Forum
kindly support with this dropna function not working
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
kindly support with this dropna function not working
#1
Shocked 
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.
Gribouillis write Jul-24-2023, 10:56 AM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.

Attached Files

.xlsx   PRACTICE WORK BOOK.xlsx (Size: 9.65 KB / Downloads: 91)
Reply
#2
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")

Attached Files

.xlsx   PRACTICE WORK BOOK.xlsx (Size: 9.65 KB / Downloads: 34)
Reply
#3
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?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Exclamation Function Not Working Alivegamer 7 1,886 Jul-19-2022, 01:03 PM
Last Post: deanhystad
  try function working on PC but not raspberry pi AnotherSam 1 1,545 Oct-11-2021, 04:51 AM
Last Post: bowlofred
  Dropna not working WiPi 3 9,643 May-15-2020, 01:26 PM
Last Post: WiPi
  Kindly asking for help with Pywinauto louloulou 0 2,032 Mar-05-2020, 06:55 PM
Last Post: louloulou
  Input() function not working in VS Code darpInd 7 13,360 Feb-17-2020, 03:28 PM
Last Post: snippsat
  Unrelated new function causing existing working function to fail Nick_G 2 2,275 Jan-27-2020, 07:21 PM
Last Post: Nick_G
  User Defined function not working Raj_Kumar 4 3,024 Dec-17-2019, 12:44 PM
Last Post: buran
  Function not working eglaud 0 1,649 Oct-24-2019, 03:30 PM
Last Post: eglaud
  Kindly explain this error BlackList96 4 4,296 Jul-16-2019, 12:46 PM
Last Post: snippsat
  Invoking function in if else statement, not working! ibaad1406 13 5,648 May-30-2019, 09:05 PM
Last Post: ibaad1406

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020