0

df 是一个由 pandas 创建的对象,它包含 13 列数据,我想通过创建新问题将两列中的数据输入到 JIRA 中。它是一个 272X13 的对象。每列代表 JIRA 中问题的不同字段。在 JIRA 中创建的每个新问题都应仅从 df 中的两列获取信息:摘要和评论。

当我在 for 循环中遍历每一行时,如何从两列中提取每个值?我只想要每一行和每一列的字符串值,没有索引,没有对象。我的代码如下:

from jira.client import JIRA
import pandas as pd

df = pd.read_csv('C:\\Python27\\scripts\\export.csv')

# Set the column names from the export.csv file equal to variables using the      
# pandas python module

# Loop to create new issues

for row in df.iterrows():
summ = str(df.loc[row.index, 'Summary'])[:30]
comments = str(df.loc[row.index, 'Comments'])
jira.create_issue(project={'key': 'DEL'}, summary=summ, description=comments, issuetype={'name': 'Bug'})

当我这样做时,我得到了错误:

Traceback (most recent call last):
File "C:\Python27\CQPython\cqpython-read-only\src\clearquest\JIRAprocess_Delta.py",    line 86, in <module>
summ = str(df.loc[row.index, 'Summary'])[:30]
File "C:\Python27\CQPython\cqpython-read-only\src\clearquest\pandas\core\indexing.py", line 669, in __getitem__
return self._getitem_tuple(key)
File "C:\Python27\CQPython\cqpython-read-only\src\clearquest\pandas\core\indexing.py", line 252, in _getitem_tuple
return self._getitem_lowerdim(tup)
File "C:\Python27\CQPython\cqpython-read-only\src\clearquest\pandas\core\indexing.py", line 361, in _getitem_lowerdim
section = self._getitem_axis(key, axis=i)
File "C:\Python27\CQPython\cqpython-read-only\src\clearquest\pandas\core\indexing.py", line 758, in _getitem_axis
return self._get_label(key, axis=axis)
File "C:\Python27\CQPython\cqpython-read-only\src\clearquest\pandas\core\indexing.py", line 60, in _get_label
return self.obj._xs(label, axis=axis, copy=True)
File "C:\Python27\CQPython\cqpython-read-only\src\clearquest\pandas\core\frame.py", line 2281, in xs
loc = self.index.get_loc(key)
File "C:\Python27\CQPython\cqpython-read-only\src\clearquest\pandas\core\index.py", line 755, in get_loc
return self._engine.get_loc(key)
File "index.pyx", line 130, in pandas.index.IndexEngine.get_loc (pandas\index.c:3238)
File "index.pyx", line 147, in pandas.index.IndexEngine.get_loc (pandas\index.c:3085)
File "index.pyx", line 293, in pandas.index.Int64Engine._check_type (pandas\index.c:5393)
File "C:\Python27\CQPython\cqpython-read-only\src\clearquest\pandas\core\series.py", line 523, in __hash__
raise TypeError('unhashable type')
TypeError: unhashable type
TypeError: unhashable type

以下是在评论字段中创建的每个问题在 JIRA 中显示的一些示例数据:

问题 1:
0 NaN
1 发现 Delta 会泄漏接收到的数据包...
2 每次断开连接时 Delta 都会重置...
3 NaN
4 它应该在 CP 进入 l 时记录...
5 通过升级 IDS 时BioMed 菜单,th...
6 通过 BioMed 菜单
升级 IDS 后,th... 7 通过 BioMed 菜单升级 IDS 后,th...
8 增加 Fusion 堆大小和 SCC1 Initia...
9 重新检查使用 build 142+,在 Matt 交付后 ...
10 使用 WPA2 时,有 EAPOL 密钥交换 go...
11 使用 WPA2 时,有 EAPOL 密钥交换 go...
12 NaN
13 NaN
14 NaN
...

我只希望每个问题都有自己的字符串值,而不是索引号或 NaN 显示如下:


问题1:
问题2:发现Delta会泄漏接收到的数据包...
问题3:每次断开连接时Delta都会重置
......

4

1 回答 1

0

问题在于 iterrows 的使用。

从文档http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.iterrows.html ),函数 df.iterrows() 以(index, Series) 对的形式迭代 DataFrame 行。

您需要的是用“row [0]”替换row.index,它为您提供您正在迭代的数据框的索引

for row in df.iterrows():
    summ = str(df.loc[row[0], 'Summary'])[:30]
    comments = str(df.loc[row[0], 'Comments'])

顺便说一句,我认为你根本不需要 iterrows:

for row_index in df.index:
    summ = str(df.loc[row_index, 'Summary'])[:30]
    comments = str(df.loc[row_index, 'Comments'])
于 2016-06-13T13:45:44.013 回答