I have a Panda Dataframe where first column contain Companies name with unique code, I want to split the string from the first space and keep right part of split but if string doesn't have any space either drop that row(preferred) or skip.
file = pd.ExcelFile("MIS REPORT.xlsx")
items = file.sheet_names
#loading different excel tabs
for i in items:
locals()["book_"+str(i)] = pd.read_excel("MIS REPORT.xlsx",sheet_name=i)
#Manking new column with company name
for i in items:
locals()["book_"+str(i)]["Firm"] = locals()["book_"+str(i)].iloc['B'].str.split(" ", 1)[1]
=> PRODUCES ERROR:
ValueError: Length of values (1) does not match length of index (202)
Input DATAFRAME
In [2]: df
Out[2]:
A B
1 Company
2 _______________________...
3 NaN
4 *Non CGR*
5 COM1014 DFIT
6 COM1380 SD RETAIL PVT LTD
7 COM3151 LUPIN LIMITED
8 COM9622 NETSURF COMMUNICATIONS (P) LTD.(MH)
9 COMCRUS CARUS LABORATORIES PVT. LTD.
Desired DATAFRAME
In [3]: df
Out[3]:
A B
4 CGR*
5 DFIT
6 SD RETAIL PVT LTD
7 LUPIN LIMITED
8 NETSURF COMMUNICATIONS (P) LTD.(MH)
9 CARUS LABORATORIES PVT. LTD.
I don't want to use Apply() function, is there any way to catch error and work around it.