简体   繁体   English

python中两个值之间的差异问题

[英]Problem with difference between two value in python

i'm starting learning python and during an analysis i need to computer every value in a column with any other value in the same column with 100005 entry.我开始学习python,在分析过程中,我需要用100005条目的同一列中的任何其他值计算列中的每个值。 I was thinking about using a code like this:我正在考虑使用这样的代码:

for ts in u.trajectory:
  distanza5=distance_array(coppia5.positions, coppia5.positions)
  k=pd.DataFrame(distanza5)
  valore=k.iat[0,1]
  list.append(valore) 

step1 = pd.concat([dist1,dist2], axis=1) step1 = pd.concat([dist1,dist2],axis=1)

step2 = pd.concat([step1,dist3], axis=1) step2 = pd.concat([step1,dist3],axis=1)

step3 = pd.concat([step2,dist4], axis=1) step3 = pd.concat([step2,dist4],axis=1)

step4 = pd.concat([step3,dist5], axis=1) step4 = pd.concat([step3,dist5],axis=1)

 for i in step4.index:
   first=step4.iat[i,0]  
   y=abs(first)
   for n in range (0,100004):
     x=abs(step4.iat[n,0])
     value[n]=y-x
     list.append(value[n])
   diff[i]=pd.DataFrame(list)
   del list

But when i try to run it i get this error: TypeError: 'numpy.float64' object does not support item assignment但是当我尝试运行它时,我收到此错误:TypeError: 'numpy.float64' object does not support item assignment

The problem is given from the value[n] line.What can i do?问题来自 value[n] 行。我该怎么办?

Thanks for your help谢谢你的帮助

For the second part of your code, the problem seems to be caused by the initialization of your value variable.对于代码的第二部分,问题似乎是由value变量的初始化引起的。 This variable probably is initialized as a single float, for example by writing value = k.iat[0,1] .该变量可能被初始化为单个浮点数,例如通过写入value = k.iat[0,1] In order as intended you have to use a list or some indexable data structure.为了按预期,您必须使用列表或一些可索引的数据结构。 For example you can get rid of the value variable as shown in the following code:例如,您可以删除value变量,如下面的代码所示:

 for i in step4.index:
   first=step4.iat[i,0]  
   y=abs(first)
   list = []
   for n in range (0,100004):
     x=abs(step4.iat[n,0])
     list.append(y-x)
   diff[i]=pd.DataFrame(list)

To solve the declaration problem of the diff variable, you can add this line diff = pd.DataFrame() before the for i in step4.index line and also change the last line of the code in this way:为了解决diff变量的声明问题,你可以for i in step4.index行的for i in step4.index之前添加这行diff = pd.DataFrame() ,也可以这样修改最后一行代码:

diff = pd.DataFrame()
for i in step4.index:
   first=step4.iat[i,0]  
   y=abs(first)
   list = []
   for n in range (0,100004):
     x=abs(step4.iat[n,0])
     list.append(y-x)
   diff.loc[:,i] = pd.Series(list)

This new last line adds the entire list in a new column inside the diff dataframe created这个新的最后一行将整个列表添加到创建的 diff 数据帧内的新列中

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

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