簡體   English   中英

pandas DataFrame based on an existing column data 添加新列時的NaN值

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

我正在嘗試在 pandas DataFrame 中創建一個基於另一個現有列的新列。 我從Name列中提取字符10:19並將其添加為新列expiry 但是大多數expiry的數據都顯示為nan 我是 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]

在處理列中的字符串並對其進行操作時,請嘗試以下操作:

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

The.str 允許您對列進行字符串操作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM