【问题标题】:Append new column to df from list using python使用python从列表中将新列附加到df
【发布时间】:2016-10-25 19:36:50
【问题描述】:

我有列表lst = ['vk.com', 'facebook.com', 'avito.ru', 'twitter.com'] 我想把它添加到这个df

date    id  request
2016-06-17 09:26:18 yans.bouts@yandex.ru    GET HTTP/1.1
2016-06-17 09:38:18 yans.bouts@yandex.ru    GET HTTP/1.1
2016-06-17 10:13:44 yans.bouts@yandex.ru    GET HTTP/1.1
2016-06-17 10:19:24 yans.bouts@yandex.ru    GET HTTP/1.1

我尝试df.append,但它不起作用。

【问题讨论】:

    标签: python list pandas dataframe append


    【解决方案1】:

    如果列表的lengthdf 的长度相同,则使用:

    lst = ['vk.com', 'facebook.com', 'avito.ru', 'twitter.com']
    df['new'] = lst
    
    print (df)
                      date                    id       request           new
    0  2016-06-17 09:26:18  yans.bouts@yandex.ru  GET HTTP/1.1        vk.com
    1  2016-06-17 09:38:18  yans.bouts@yandex.ru  GET HTTP/1.1  facebook.com
    2  2016-06-17 10:13:44  yans.bouts@yandex.ru  GET HTTP/1.1      avito.ru
    3  2016-06-17 10:19:24  yans.bouts@yandex.ru  GET HTTP/1.1   twitter.com
    

    【讨论】:

      猜你喜欢
      • 2021-03-02
      • 1970-01-01
      • 1970-01-01
      • 2016-09-17
      • 2021-11-13
      • 2020-09-29
      • 1970-01-01
      • 2017-12-22
      • 1970-01-01
      相关资源
      最近更新 更多