Python Forum
Pandas Data frame column condition check based on length of the value
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Pandas Data frame column condition check based on length of the value
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):
    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)  
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.

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

Possibly Related Threads…
Thread Author Replies Views Last Post
  Python Based ETL Using Pandas HyperSpeed 0 71 Jan-13-2022, 04:18 PM
Last Post: HyperSpeed
Question Sorting data with pandas TheZaind 4 524 Nov-22-2021, 07:33 PM
Last Post: aserian
  New Dataframe Column Based on Several Conditions nb1214 1 345 Nov-16-2021, 10:52 PM
Last Post: jefsummers
  pandas: Compute the % of the unique values in a column JaneTan 1 477 Oct-25-2021, 07:55 PM
Last Post: jefsummers
  Exporting data frame to excel dyerlee91 0 437 Oct-05-2021, 11:34 AM
Last Post: dyerlee91
  What if a column has about 90% of data as outliers? Asahavey17 1 571 Aug-23-2021, 04:55 PM
Last Post: jefsummers
  update values in one dataframe based on another dataframe - Pandas iliasb 2 2,040 Aug-14-2021, 12:38 PM
Last Post: jefsummers
  How to move each team row to a new column. Pandas vladiwnl 0 674 Jun-13-2021, 08:10 AM
Last Post: vladiwnl
  Adding a new column to a Panda Data Frame rsherry8 2 765 Jun-06-2021, 06:49 PM
Last Post: jefsummers
Question [Solved] How to refer to dataframe column name based on a list lorensa74 1 941 May-17-2021, 07:02 AM
Last Post: lorensa74

Forum Jump:

User Panel Messages

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