[英]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.我正在尝试在 pandas DataFrame 中创建一个基于另一个现有列的新列。 I am extracting characters
10:19
from column Name
and adding it as a new column expiry
.我从
Name
列中提取字符10:19
并将其添加为新列expiry
。 But most of the datas in expiry
are showing as nan
.但是大多数
expiry
的数据都显示为nan
。 I am new to python and Pandas. How can I solve this?我是 python 和 Pandas 的新用户,我该如何解决?
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]
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. The.str 允许您对列进行字符串操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.