Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

NaN values when new column is added to pandas DataFrame based on an existing column data

I am trying to create a new column in pandas DataFrame which is based on another existing column. I am extracting characters 10:19 from column Name and adding it as a new column expiry . But most of the datas in expiry are showing as nan. I am new to python and Pandas. How can I solve this ?

allowedSegment = [14]
index_symbol = "BANKNIFTY"

fno_url = 'http://public.fyers.in/sym_details/NSE_FO.csv'
fno_symbolList = pd.read_csv(fno_url, header=None)
fno_symbolList.columns = ['FyersToken', 'Name', 'Instrument', 'lot', 'tick', 'ISIN', 'TradingSession', 'Lastupdatedate',
                           'Expirydate', 'Symbol', 'Exchange', 'Segment', 'ScripCode', 'ScripName', 'Ignore_1',
                          'StrikePrice', 'CE_PE', 'Ignore_2']

fno_symbolList = fno_symbolList[fno_symbolList['Instrument'].isin(allowedSegment) & (fno_symbolList['ScripName'] == index_symbol)]

fno_symbolList['expiry'] = fno_symbolList['Name'][10:19]

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

When dealing with strings in columns and doing operations on it, try the following:

fno_symbolList['expiry'] = fno_symbolList['Name'].str[10:19]

The .str allows you to do string operations on columns.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading