Python Forum

Full Version: Pandas Data frame column condition check based on length of the value
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have pandas data frame which gets created by reading an excel file. The excel file has a column called serial number. Then I pass a serial number to another function which connect to API and fetch me the result set for those serial number.

def create_excel(filename):
try:
    data = pd.read_excel(filename, usecols=[4,18,19,20,26,27,28],converters={'Serial Number': '{:0>32}'.format})
except Exception as e:
    sys.exit("Error reading %s: %s" % (filename, e))

data["Subject Organization"].fillna("N/A",inplace= True)
df = data[data['Subject Organization'].str.contains("Fannie",case = False)]
#df['Serial Number'].apply(lamda x:  '000'+x  if  len(x) == 29 else  '00'+x if len(x) == 30 else '0'+x if len(x) == 31 else x)  
print(df)
df.to_excel(r'Data.xlsx',index= False)
output = df['Serial Number'].apply(lambda x: fetch_by_ser_no(x))
df2 = pd.DataFrame(output)
df2.columns = ['Output']
df5 = pd.concat([df,df2],axis = 1)
The problem I am facing is I want to check if data frame returned by fetch_by_ser_no() is blank then make the serial number as 34 characters by adding two more leading 00 and then check the function again.

How can I accomplish it with less dataframes.

Thanks
Hard to test without access to that function, but try this (line 12)
output = df['Serial Number'].apply(fetch_by_ser_no, axis = 1)