所以我有一个班级(带标题):
longAssNumber <- "20402000224222032203233430020222024422320202234232443443004440404332402220404233302004204202004033420200022204334434044422322400200244044322202200234200424402002224303430023432202220343420323200300022320004223040024342244424040322424340240044220302432400203033300042423340222323200020034043004"
numberVector <- as.numeric(strsplit(longAssNumber, "")[[1]])
write.csv(numberVector, "number.csv")
和一系列(没有标题):
df1=
value
B 0.000367
S 0.000486
T 0.000259
J 0.000177
我期待从数据帧中减去系列。我首先尝试直接减去它们:
ser1=
B 0.021711
S 0.023538
T 0.017815
L 0.013303
然后我尝试将系列转换为df:
B L T S value
B NaN NaN NaN NaN NaN
S NaN NaN NaN NaN NaN
T NaN NaN NaN NaN NaN
L NaN NaN NaN NaN NaN
然后我重复了这个过程,出现了这个错误:
D型细胞:float64&GT;与块值不支持的操作数类型(一个或多个),用于: - “方法”和“浮动”
我只想做一个减法df1-ser1
答案 0 :(得分:0)
df = df1.sub(ser1, axis=0)
print (df)
value
B -0.021344
J NaN
L NaN
S -0.023052
T -0.017556
也可以通过Series.to_frame
将Series
转换为DataFrame
:
df = df1.sub(ser1.to_frame('value'))
print (df)
value
B -0.021344
J NaN
L NaN
S -0.023052
T -0.017556
答案 1 :(得分:0)
您还可以通过在.to_frame
方法中添加括号并提供与数据框的列名匹配的列名来保留您目前所拥有的内容:
df1 - ser1.to_frame('value')
# value
# B -0.021344
# J NaN
# L NaN
# S -0.023052
# T -0.017556
我希望这会有所帮助。