使用python将新列附加到df from list

2024-05-18 10:52:55 发布

您现在位置:Python中文网/ 问答频道 /正文

我有清单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,但没用。你知道吗


Tags: comidhttpdfgetdatefacebookru
1条回答
网友
1楼 · 发布于 2024-05-18 10:52:55

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

相关问题 更多 >