pandas 如何在熊猫系列中用 at[] 替换 set_value
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/52770563/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I replace set_value with at[] in a pandas Series
提问by user1067305
I'm trying to construct a pandas Series to concatenate onto a dataframe.
我正在尝试构建一个Pandas系列来连接到数据帧上。
import numpy as np
import pandas as pd
rawData = pd.read_csv(input, header=1) # the DataFrame
strikes = pd.Series() # the empty Series
for i, row in rawData.iterrows():
sym = rawData.loc[i,'Symbol']
strike = float(sym[-6:])/1000
strikes = strikes.set_value(i, strike)
print("at26: ",strikes.values)
This program works, but I get the error message:
该程序有效,但我收到错误消息:
"line 25: FutureWarning: set_value is deprecated and will be removed in a future release. Please use .at[] or .iat[] accessors instead."
“第 25 行:FutureWarning:set_value 已弃用,将在未来版本中删除。请改用 .at[] 或 .iat[] 访问器。”
Every way I have tried to substitute .at, I get a syntax error. Many of the suggestions posted relate to DataFrames, not Series. Append requires another series, and complains when I give it a scalar.
我尝试用每种方式替换 .at 时,都会出现语法错误。发布的许多建议与 DataFrames 相关,而不是 Series。Append 需要另一个系列,当我给它一个标量时会抱怨。
What is the proper way to do it?
什么是正确的方法呢?
回答by jpp
Replace strikes.set_value(i, strike)
with strikes.at[i] = strike
.
替换strikes.set_value(i, strike)
为strikes.at[i] = strike
。
Note that assignment back to a series is not necessary with set_value
:
请注意,分配回系列不是必需的set_value
:
s = pd.Series()
s.set_value(0, 10)
s.at[1] = 20
print(s)
0 10
1 20
dtype: int64
For the algorithm you are looking to run, you can simply use assignment:
对于您要运行的算法,您可以简单地使用赋值:
strikes = rawData['Symbol'].str[-6:].astype(float) / 1000