1

Here is the code:

import pandas as pd

filejan = "01.xls"
filefeb = "02.xls"
roomtype = {
    'Room Type Code': ['DLK', 'DTN', 'DTP', 'DKP', 'PTG', 'PKG', 'PTP', 'PKP', 'PKL', 'PTL', 'FPK', 'DLS', 'PRS', 'AVS', 'ASW', 'ASP', 'AVP', 'BFS', 'AVR'],
    'Room Type': ['Deluxe', 'Deluxe', 'Deluxe Pool', 'Deluxe Pool', 'Premiere', 'Premiere', 'Premiere', 'Premiere', 'Lagoon', 'Lagoon', 'Family', 'DL Suite', 'PR Suite', 'AS No Pool', 'AS Whirl Pool', 'AS Private Pool', 'Villa', 'Beach Front', 'Residence']
}
mtdjan = pd.read_excel(filejan)
mtdfeb = pd.read_excel(filefeb)
dataRoomType = pd.DataFrame(roomtype, columns=['Room Type Code', 'Room Type'])


def cleanJan(dataFebruary):
    dataFebruary.drop([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], axis=0)
    dataFebruary.drop(["Unnamed: 3", "Unnamed: 4", "Unnamed: 10", "Unnamed: 13",
                       "Page -1 of 1", "Unnamed: 6", "Unnamed: 2"], axis=1)
    dataFebruary.dropna()
    dataFebruary.rename(
        columns={
            "The xxx Beach Resorts Bali": "Date Create",
            "Unnamed: 1": "Name",
            "Unnamed: 5": "Room Type Code",
            "Unnamed: 7": "Arrival",
            "RESERVATION LIST": "Departure",
            "Unnamed: 9": "Rate Code",
            "Unnamed: 11": "Rate",
            "Unnamed: 12": "Company",
            "Unnamed: 14": "Segment",
            "Unnamed: 15": "Status",
        },
    )


df1 = cleanJan(mtdjan)
df2 = cleanJan(mtdfeb)

print(df1)

and this is the terminal

==============================================

WARNING *** file size (1195377) not 512 + multiple of sector size (512)
WARNING *** OLE2 inconsistency: SSCS size is 0 but SSAT size is non-zero
*** No CODEPAGE record, no encoding_override: will use 'iso-8859-1'
*** No CODEPAGE record, no encoding_override: will use 'iso-8859-1'
*** No CODEPAGE record, no encoding_override: will use 'iso-8859-1'
WARNING *** file size (1228825) not 512 + multiple of sector size (512)
WARNING *** OLE2 inconsistency: SSCS size is 0 but SSAT size is non-zero
*** No CODEPAGE record, no encoding_override: will use 'iso-8859-1'
*** No CODEPAGE record, no encoding_override: will use 'iso-8859-1'
*** No CODEPAGE record, no encoding_override: will use 'iso-8859-1'
**None**
==========================================
ForceBru
  • 43,482
  • 10
  • 63
  • 98
AK555666
  • 17
  • 5

1 Answers1

1

Many pandas functions do not modify the df it is called on, but return a modified df. Generally you should either use inplace=True argument if available, or use

df = df.some_function(..) 
... 
return df

construct in your function. For example, the dropna call in your function should be modified as follows

dataFebruary.dropna(inplace = True)

and similar for rename and drop

Edit

Your function code looks correct except, and I missed it on the first pass, it did not return anything so df1 = ... would just have df1 = None.

So I would modify the function as follows -- first it will have a return value and second would not use inplace arguments, as somewhat more 'Pythonic':

def cleanJan(dataFebruary):
    df = dataFebruary.drop([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], axis=0)
    df = df.drop(["Unnamed: 3", "Unnamed: 4", "Unnamed: 10", "Unnamed: 13",
                       "Page -1 of 1", "Unnamed: 6", "Unnamed: 2"], axis=1)
    df = df.dropna()
    df = df.rename(
        columns={
            "The xxx Beach Resorts Bali": "Date Create",
            "Unnamed: 1": "Name",
            "Unnamed: 5": "Room Type Code",
            "Unnamed: 7": "Arrival",
            "RESERVATION LIST": "Departure",
            "Unnamed: 9": "Rate Code",
            "Unnamed: 11": "Rate",
            "Unnamed: 12": "Company",
            "Unnamed: 14": "Segment",
            "Unnamed: 15": "Status",
        },
    )
    return df


df1 = cleanJan(mtdjan)
df2 = cleanJan(mtdfeb)

print(df1)
piterbarg
  • 8,089
  • 2
  • 6
  • 22
  • Thanks for taking your time to answer it, but what about code structure of the function I wrote, is it correct? Thanks – AK555666 Mar 12 '21 at 13:13
  • ah I missed the fact that your function actually did not return anything. see my edit, let me know if it works. As you did not provide a minimal reproducible example I can't really test. For your next question pls check out this [mcve] and [pandas](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) – piterbarg Mar 12 '21 at 13:37
  • Thank you very much, I was stuck for almost 1 night. now I can put my head at rest. once again, thank you very much. – AK555666 Mar 12 '21 at 14:09