如何将另一列的值初始化后添加到数据框中的对象列?

huangapple go评论78阅读模式
英文:

How do I add a column of objects to a dataframe initialized by values from another column?

问题

我有一个从CSV文件中提取的股票符号表格:

      Symbol      Last      Open %Change     ATR Earnings Date                  Sector
153        WHR    138.02    139.21  -2.26%    4.50       01 / 25  消费者自由裁量权
154        WIX     76.86     80.05  -5.33%    4.01       02 / 07  信息技术
155         WM    158.03    158.60  -0.78%    3.10       02 / 01             工业
156        WMT    143.09    144.77  -1.44%    2.58       02 / 07        消费者食品
157        YUM    127.71    128.50  -0.83%    2.17       02 / 07  消费者自由裁量权

我想为每个符号构建一个TaData类的数据对象,并将其添加到一个新列中。

通常,单个符号的TaData构造如下所示:

data = TaData('GOOG')

我想要类似于以下的操作(因为它传递了整个列而不是单独的符号,所以不起作用):

df = pd.read_csv('watchlist.csv')
df['ta_data'] = TaData(df['Symbol'])

我的唯一选项是使用for循环吗?

英文:

I have a table of stock symbols I'm pulling from a csv file:

      Symbol      Last      Open %Change     ATR Earnings Date                  Sector
153        WHR    138.02    139.21  -2.26%    4.50       01 / 25  Consumer Discretionary
154        WIX     76.86     80.05  -5.33%    4.01       02 / 07  Information Technology
155         WM    158.03    158.60  -0.78%    3.10       02 / 01             Industrials
156        WMT    143.09    144.77  -1.44%    2.58       02 / 07        Consumer Staples
157        YUM    127.71    128.50  -0.83%    2.17       02 / 07  Consumer Discretionary

And I want to construct data objects of class TaData for each symbol and add it to a new column.

TaData for a single symbol is normally constructed like this:

data = TaData('GOOG')

I want something like this (that didn't work since it passed in the whole column instead of individual symbols):

df = pd.read_csv('watchlist.csv')
df['ta_data'] = TaData(df['Symbol'])

Is a for loop my only option?

答案1

得分: 0

我认为pandas的apply函数对你会很有用。在这里找到更多关于pandas的信息。

你可以尝试以下操作:

df['ta_data'] = df['Symbol'].apply(lambda x: TaData(x))
英文:

I think pandas apply function would be useful for you. Find more about pandas here

You can try the following:-

df['ta_data'] = df['Symbol'].apply(lambda x: TaData(x))

huangapple
  • 本文由 发表于 2023年6月9日 07:23:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/76436292.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定