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
  Grouping in pandas/multi-index data frame Aleqsie 3 563 Jan-06-2024, 03:55 PM
Last Post: deanhystad
  HTML Decoder pandas dataframe column mbrown009 3 932 Sep-29-2023, 05:56 PM
Last Post: deanhystad
  Filtering Data Frame, with another value NewBiee 9 1,297 Aug-21-2023, 10:53 AM
Last Post: NewBiee
  Supervised learning, tree based model - problems splitting data Pixel 0 629 May-16-2023, 05:25 PM
Last Post: Pixel
  Grouping Data based on 30% bracket purnima1 0 929 Feb-16-2023, 07:14 PM
Last Post: purnima1
  Make unique id in vectorized way based on text data column with similarity scoring ill8 0 855 Dec-12-2022, 03:22 AM
Last Post: ill8
Smile How to further boost the data read write speed using pandas tjk9501 1 1,215 Nov-14-2022, 01:46 PM
Last Post: jefsummers
  pandas column percentile nuncio 7 2,357 Aug-10-2022, 04:41 AM
Last Post: nuncio
  reduce time series based on sum condition amdi40 0 1,068 Apr-06-2022, 09:09 AM
Last Post: amdi40
  Pandas Dataframe Filtering based on rows mvdlm 0 1,381 Apr-02-2022, 06:39 PM
Last Post: mvdlm

Forum Jump:

User Panel Messages

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