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发现三角洲会泄漏包裹...
2每次断开连接时,Delta都会重置......
3 NaN
4当CP需要时,它应该被记录...
5通过BioMed菜单升级IDS后,...
6通过BioMed菜单升级IDS后,...
7通过BioMed菜单升级IDS后,...
8增加Fusion堆大小和SCC1 Initia ......
9在Matt交付之后,使用build 142+重新检查......
10使用WPA2时,有EAPOL密钥交换...
11使用WPA2时,有EAPOL密钥交换...
12 NaN
13 NaN
14 NaN
......
我只希望每个问题都有自己的字符串值,而不是像这样显示的索引号或NaN:
问题1:
问题2:发现Delta会泄漏数据包接收...
问题3:每次断开连接时Delta会重置......
......
答案 0 :(得分:0)
问题在于使用iterrows。
从文档http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.iterrows.html),函数df.iterrows()以(索引,系列)对迭代DataFrame行。
您需要将row.index替换为“row [0]”,它为您提供迭代的数据帧的索引
for row in df.iterrows():
summ = str(df.loc[row[0], 'Summary'])[:30]
comments = str(df.loc[row[0], 'Comments'])
顺便说一句,我认为你根本不需要它:
for row_index in df.index:
summ = str(df.loc[row_index, 'Summary'])[:30]
comments = str(df.loc[row_index, 'Comments'])