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

Time:01-05

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]

CodePudding user response:

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.

  • Related