使用python从列表中向df追加新列

时间:2016-06-23 12:48:00

标签: python list pandas dataframe append

我有列表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,但它不起作用。

1 个答案:

答案 0 :(得分:4)

如果列表的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