我有一个数据帧df
:
AuthorID Year citations
0 1 1995 86
1 2 1995 22
2 3 1995 22
3 4 1995 22
4 5 1995 36
5 6 1995 25
以及另一个我创建并初始化为0的数据帧df2
,其中每个索引表示来自df
的AuthorID
:
现在我要做的是迭代df
,并将引文值添加到第二个矩阵中的正确位置。因此,如果我要根据上面的内容填充df2
,它将如下所示:
1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004
1 0 86 0 0 0 0 0 0 0 0 0
2 0 22 0 0 0 0 0 0 0 0 0
3 0 22 0 0 0 0 0 0 0 0 0
4 0 36 0 0 0 0 0 0 0 0 0
5 0 25 0 0 0 0 0 0 0 0 0
6 0 0 0 0 0 0 0 0 0 0 0
这很简单。在
现在我所做的是:
for index, row in df.iterrows():
df2.iloc[row[0]][row[1]] = df2.iloc[row[0]][row[1]] + row[2]
但它总是给我以下信息:
IndexError: index out of bounds
所以我试着:
for index, row in df.iterrows():
df2.at[row[0], row[1]] = df2.at[row[0], row[1]] + row[2]
它给了我:
ValueError: At based indexing on an non-integer index can only have non-integer indexers
我也试过df.iat
,但也没用。在
不知道我做错了什么。当我检查df.dtypes
时,它们都返回了int64
所以,这里有一个很长的路要做:为每个作者指定1/3的值,而不是1995年。在
x
是您的数据帧。在我们将为下面的每个作者添加年份:1996、1997和1998,并存储在
y
数据框中。在下面,我们将把1995年引文量的1/3分配给同一作者的所有其他年份。在
^{pr2}$为什么不能像这样旋转第一个数据帧
这将把所有年份作为列,索引将是您的
AuthorID
。在相关问题 更多 >
编程相关推荐