简体   繁体   English

分体 Dataframe 列

[英]Split Dataframe column

I'm using yfinance to get option chain information.我正在使用 yfinance 获取期权链信息。 In the yfinance documentation it says data when retrieved would be pandas dataframe.在 yfinance 文档中,它说检索到的数据将是 pandas dataframe。

 import pandas as pd import numpy as np import yfinance as yf #retrieving for specific date data = tk.option_chain('2021-03-26')

Output looks like this: Output 看起来像这样:

在此处输入图像描述

Then I try this to split the text然后我尝试这个来分割文本

 data['ExpirationDate'] = data['contractSumbol'].str.slice(start=4, stop=10)

Output: TypeError: tuple indices must be integers or slices, not str Output:TypeError:元组索引必须是整数或切片,而不是 str

so I try to define column by integer like this:所以我尝试通过 integer 定义列,如下所示:

data['ExpirationDate'] = data[0].str.slice(start=4, stop=10) data['ExpirationDate'] = data[0].str.slice(start=4, stop=10)

Output error: AttributeError: 'DataFrame' object has no attribute 'str' Output 错误:AttributeError: 'DataFrame' object 没有属性 'str'

I try: data['ExpirationDate'] = data[0].slice(start=4, stop=10)我尝试: data['ExpirationDate'] = data[0].slice(start=4, stop=10)

but get this error: AttributeError: 'DataFrame' object has no attribute 'slice'但得到这个错误: AttributeError: 'DataFrame' object has no attribute 'slice'

End goal is to have a dataframe with just the expiration dates formatted as dates:最终目标是拥有一个 dataframe ,其到期日期格式为日期:

在此处输入图像描述

It seems that option_chain() return an object of type yfinance.ticker.Options which contains a collections of pandas Dataframes似乎 option_chain() 返回一个 yfinance.ticker.Options 类型的yfinance.ticker.Options ,其中包含 pandas 数据帧的 collections

type(data)

yfinance.ticker.Options yfinance.ticker.Options

type(data[0])

pandas.core.frame.DataFrame pandas.core.frame.DataFrame

data[0]

contractSymbol  lastTradeDate   strike  lastPrice   bid ask change  percentChange   volume  openInterest    impliedVolatility   inTheMoney  contractSize    currency
0   MSFT210326C00140000 2021-03-15 04:09:20 140.0   93.03   91.50   92.15   0.000000    0.000000    NaN 0   1.819337    True    REGULAR USD
1   MSFT210326C00145000 2021-03-16 19:35:26 145.0   93.87   86.40   87.20   0.000000    0.000000    2.0 0   1.695314    True    REGULAR USD
2   MSFT210326C00170000 2021-02-09 14:40:03 170.0   72.75   67.80   68.65   0.000000    0.000000    NaN 0   2.339848    True    REGULAR USD
3   MSFT210326C00175000 2021-03-01 20:24:42 175.0   57.80   56.45   57.10   0.000000    0.000000    40.0    28  1.082524    True    REGULAR USD
4   MSFT210326C00180000 2021-02-18 16:22:29 180.0   51.00   51.40   52.35   -6.450001   -11.227155  1.0 4   1.024419    True    REGULAR USD
5   MSFT210326C00190000 2021-03-04 16:58:48 190.0   49.65   41.50   42.25   0.000000    0.000000    1.0 59  0.839845    True    REGULAR USD
6   MSFT210326C00195000 2021-03-18 13:41:08 195.0   38.75   36.65   37.30   0.000000    0.000000    1.0 0   0.774416    True    REGULAR USD

To extract date of expiration提取到期日期

df = data[0]
df["contractSymbol"].str[4:10]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM