我的数据集有列
created_at date time timezone \
0 2021-06-03 09:01:59 India Standard Time 2021-06-03 09:01:59 530
1 2021-06-03 09:01:41 India Standard Time 2021-06-03 09:01:41 530
2 2021-06-03 07:32:58 India Standard Time 2021-06-03 07:32:58 530
3 2021-06-03 07:31:55 India Standard Time 2021-06-03 07:31:55 530
4 2021-06-03 06:00:52 India Standard Time 2021-06-03 06:00:52 530
tweet
0 "Advertisers offering #cryptocurrency exchange...
1 Beijing to Disperse $6 Million in Digital Yuan...
2 “Combating ransomware is a priority for the ad...
3 Guggenheim has registered a fund with the SEC ...
4 The most recent 2009 spend moved last year, an...
数据集的完整链接是here。我想根据日期创建列。与2021-06-03日期一样,每个标题都应该有一列,以此类推。有关我希望如何使用这些列的参考信息,请参见Combined_news.csv文件中的here。下面的代码是我的方法
from collections import defaultdict
d = defaultdict(list)
i = 1
j = 1
while i < btc_news.shape[0]:
if btc_news.loc[i, 'date'] == btc_news.loc[i-1, 'date']:
temp = 'headline' + str(j)
d[temp].append(btc_news.loc[i-1, 'tweet'])
j += 1
i += 1
continue
else:
temp = 'headline' + str(j)
d[temp].append(btc_news.loc[i-1, 'tweet'])
d['date'].append(btc_news.loc[i-1, 'date'])
j = 1
i += 1
我希望以我在Combined_News.csv中提到的方式输出。我想在不同的列中包含该特定日期的所有标题以及日期列。我希望你能理解我的问题
可能不是最有效的解决方案,但这是可行的
首先,您
groupby
指定日期并连接一个日期的所有tweet:接下来,将列表拆分为各个列:
输出格式为:
相关问题 更多 >
编程相关推荐