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
#1
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
Reply
#2
Hard to test without access to that function, but try this (line 12)
output = df['Serial Number'].apply(fetch_by_ser_no, axis = 1)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Smile How to further boost the data read write speed using pandas tjk9501 1 121 Nov-14-2022, 01:46 PM
Last Post: jefsummers
  pandas column percentile nuncio 7 873 Aug-10-2022, 04:41 AM
Last Post: nuncio
  reduce time series based on sum condition amdi40 0 490 Apr-06-2022, 09:09 AM
Last Post: amdi40
  Pandas Dataframe Filtering based on rows mvdlm 0 795 Apr-02-2022, 06:39 PM
Last Post: mvdlm
Thumbs Up can't access data from URL in pandas/jupyter notebook aaanoushka 1 913 Feb-13-2022, 01:19 PM
Last Post: jefsummers
  Python Based ETL Using Pandas HyperSpeed 0 819 Jan-13-2022, 04:18 PM
Last Post: HyperSpeed
Question Sorting data with pandas TheZaind 4 1,411 Nov-22-2021, 07:33 PM
Last Post: aserian
  New Dataframe Column Based on Several Conditions nb1214 1 1,144 Nov-16-2021, 10:52 PM
Last Post: jefsummers
  pandas: Compute the % of the unique values in a column JaneTan 1 1,189 Oct-25-2021, 07:55 PM
Last Post: jefsummers
  Exporting data frame to excel dyerlee91 0 1,049 Oct-05-2021, 11:34 AM
Last Post: dyerlee91

Forum Jump:

User Panel Messages

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