Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 熊猫-将列表添加到多列(对于多行)_Python_Pandas - Fatal编程技术网

Python 熊猫-将列表添加到多列(对于多行)

Python 熊猫-将列表添加到多列(对于多行),python,pandas,Python,Pandas,我有一个要更新为多列的值列表,这对于一行来说很好。但是,当我尝试更新多行时,它只是用最后一个值覆盖整个列 每行的列表如下所示(注意:列表长度大小可变): 我可以使用以下方法将其存储在空列中: for i in range(len(trans_dates)): df[('T' + str(i + 1) + ' - Date')] = trans_dates[i] 但是,这会使用单个trans\u dates[i]值更新整个列 我原以为用上面的代码在每一行上循环会起作用,但它仍然会覆盖 f

我有一个要更新为多列的值列表,这对于一行来说很好。但是,当我尝试更新多行时,它只是用最后一个值覆盖整个列

每行的列表如下所示(注意:列表长度大小可变):

我可以使用以下方法将其存储在空列中:

for i in range(len(trans_dates)):
    df[('T' + str(i + 1) + ' - Date')] = trans_dates[i]
但是,这会使用单个
trans\u dates[i]
值更新整个列

我原以为用上面的代码在每一行上循环会起作用,但它仍然会覆盖

for issues in all_issues:
    for i in range(len(trans_dates)):
        df[('T' + str(i + 1) + ' - Date')] = trans_dates[i]
  • 如何仅更新循环中的当前行?
  • 我这样做对吗?还是有一种更快的矢量化方法?
下面是完整的代码片段:

for issues in all_issues:
    print(issues)
    changelog = issues.changelog
    trans_dates = []
    from_status = []
    to_status = []
    for history in changelog.histories:
        for item in history.items:
            if item.field == 'status':
                trans_dates.append(history.created[:19])
                from_status.append(item.fromString)
                to_status.append(item.toString)
    trans_dates = list(reversed(trans_dates))
    from_status = list(reversed(from_status))
    to_status = list(reversed(to_status))
    print(trans_dates)

    # Store raw data in created columns and convert dates to pd.to_datetime
    for i in range(len(trans_dates)):
        df[('T' + str(i + 1) + ' - Date')] = trans_dates[i]
    for i in range(len(to_status)):
        df[('T' + str(i + 1) + ' - To')] = to_status[i]
    for i in range(len(from_status)):
        df[('T' + str(i + 1) + ' - From')] = from_status[i]
    for i in range(len(trans_dates)):
        df['T' + str(i + 1) + ' - Date'] = pd.to_datetime(df['T' + str(i + 1) + ' - Date'])
  • 编辑:添加样本输入和输出。
输入: 发行/行#1列表(注意年度变化):

问题2

第3期

问题4

输出:

        col       T1                     T2                      T3                 T4
        17 '2016-03-16T09:53:05'   '2016-03-16T16:13:33'  '2016-03-17T13:30:31'  '2016-03-17T13:30:31'
        18 '2017-03-16T09:53:05'   '2017-03-16T16:13:33'  '2017-03-17T13:30:31'  np.nan
        19 '2018-03-16T09:53:05'   '2018-03-16T16:13:33'  '2018-03-17T13:30:31' np.nan
        20 '2015-03-16T09:53:05'   '2015-03-16T16:13:33'      np.nan     np.nan
与此相反:

for i in range(len(trans_dates)):
    df[('T' + str(i + 1) + ' - Date')] = trans_dates[i]
试试这个:

for i in range(len(trans_dates)):
    df.loc[i, ('T' + str(i + 1) + ' - Date')] = trans_dates[i]  

也许有更好的方法可以做到这一点
df.merge
df.replace
出现在脑海中。。。如果您发布了输入数据框的外观以及预期结果,这将非常有用

您能发布一个输入和预期输出的示例吗?添加到主要问题中。
['2018-03-16T09:53:05',
 '2018-03-16T16:13:33',
 '2018-03-17T13:30:31']
['2015-03-16T09:53:05',
 '2015-03-16T16:13:33']
        col       T1                     T2                      T3                 T4
        17 '2016-03-16T09:53:05'   '2016-03-16T16:13:33'  '2016-03-17T13:30:31'  '2016-03-17T13:30:31'
        18 '2017-03-16T09:53:05'   '2017-03-16T16:13:33'  '2017-03-17T13:30:31'  np.nan
        19 '2018-03-16T09:53:05'   '2018-03-16T16:13:33'  '2018-03-17T13:30:31' np.nan
        20 '2015-03-16T09:53:05'   '2015-03-16T16:13:33'      np.nan     np.nan
for i in range(len(trans_dates)):
    df[('T' + str(i + 1) + ' - Date')] = trans_dates[i]
for i in range(len(trans_dates)):
    df.loc[i, ('T' + str(i + 1) + ' - Date')] = trans_dates[i]