0

强制性警告 - Python 和 Pandas 的新手。

我有一个 .csv 文件,其中包含我想通过CanvasAPI运行以获取用户名的信息。.csv 布局为:

     Name  user_id
0  Name_1     7454
1  Name_2     7075
2  Name_3     7377
3  Name_4     7259
4  Name_5     7294
5  Name_6     7491

我可以毫无困难地通过 get_user 函数处理单个单元格,如下所示:

from canvasapi import Canvas
import pandas as pd

df = pd.read_csv("Test2.csv")

user = df['user_id'][0]

print(canvas.get_user(user))

输出为:

Name_1 (7454)

但是,我完全不知道如何在 Pandas 中重复行以获取类似的拉动。我的第一次代码尝试是:

users = df['user_id'][0:5]

def user_id():
    for user in users:
        print(user)

print(canvas.get_user(user_id()))

这会产生以下错误:

7454
7075
7377
7259
7294
Traceback (most recent call last):
  File "C:\Users\danie\AppData\Local\Programs\Python\Python39\lib\site-packages\canvasapi\util.py", line 116, in obj_or_id
    return int(parameter)
TypeError: int() argument must be a string, a bytes-like object or a number, not 'NoneType'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\danie\AppData\Local\Temp\atom_script_tempfiles\2021523-18696-123dxol.cjwo", line 39, in <module>
    print(canvas.get_user(user_id()))
  File "C:\Users\danie\AppData\Local\Programs\Python\Python39\lib\site-packages\canvasapi\canvas.py", line 1221, in get_user
    user_id = obj_or_id(user, "user", (User,))
  File "C:\Users\danie\AppData\Local\Programs\Python\Python39\lib\site-packages\canvasapi\util.py", line 133, in obj_or_id
    raise TypeError(message)
TypeError: Parameter user must be of type User or int.

在试图重申第二种方式时:

print(canvas.get_user(df['user_id'][0:5]))

我遇到了这个错误:

Traceback (most recent call last):
  File "C:\Users\danie\AppData\Local\Temp\atom_script_tempfiles\2021523-18696-gmkiri.t4uur", line 49, in <module>
    print(canvas.get_user(df['user_id'][0:5]))
  File "C:\Users\danie\AppData\Local\Programs\Python\Python39\lib\site-packages\canvasapi\canvas.py", line 1218, in get_user
    elif user == "self":
  File "C:\Users\danie\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas\core\generic.py", line 1442, in __nonzero__
    raise ValueError(
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

第三次尝试:

def user_id():
    for i, row in df.iterrows():
        print(f"{row[1]}\n")

user_id = pd.to_numeric(user_id())

print(canvas.get_user(user_id))

产生此错误:

7454

7075

7377

7259

7294

7491

Traceback (most recent call last):
  File "C:\Users\danie\AppData\Local\Programs\Python\Python39\lib\site-packages\canvasapi\util.py", line 116, in obj_or_id
    return int(parameter)
ValueError: cannot convert float NaN to integer

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\danie\AppData\Local\Temp\atom_script_tempfiles\2021523-18696-62ic5r.qihqf", line 41, in <module>
    print(canvas.get_user(user_id))
  File "C:\Users\danie\AppData\Local\Programs\Python\Python39\lib\site-packages\canvasapi\canvas.py", line 1221, in get_user
    user_id = obj_or_id(user, "user", (User,))
  File "C:\Users\danie\AppData\Local\Programs\Python\Python39\lib\site-packages\canvasapi\util.py", line 133, in obj_or_id
    raise TypeError(message)
TypeError: Parameter user must be of type User or int.

我对如何进行正式没有想法。关于我做错了什么以及朝着正确方向前进的任何想法?非常感谢所有帮助。

4

0 回答 0