我得到了一个txt文件的数据部分,并将其存储在一个列表中。数据应该是年份、数据1、数据2、数据3。它们在原始txt文件中用\t\t或\t分隔,因为我直接附加了数据行。现在我想把它放到一个数据框中去处理。dataframe有三列year、data1和data2。你知道吗
['2018\t \t7,107\t4,394\t2,713', '2017\t \t16,478\t10,286\t6,192', '2016\t \t15,944\t9,971\t5,973', '2015\t \t15,071\t9,079\t5,992', '2014\t \t14,415\t8,596\t5,819', '2013\t \t14,259\t8,269\t5,990', '2012\t \t14,010\t8,143\t5,867', '2011\t \t14,149\t8,126\t6,023', '2010\t \t14,505\t7,943\t6,562', '2009\t \t14,632\t8,022\t6,610', '2008\t \t14,207\t7,989\t6,218', '2007\t \t14,400\t8,085\t6,315', '2006\t \t14,750\t8,017\t6,733', '2005\t \t14,497\t7,593\t6,904', '2004\t \t14,155\t7,150\t7,005', '2003\t \t13,285\t6,457\t6,828', '2002\t \t12,821\t6,190\t6,631', '2001\t \t12,702\t6,080\t6,622', '2000\t \t11,942\t5,985\t5,957', '1999\t \t10,872\t5,824\t5,048', '2018\t \t10,362\t5,793\t4,569', '2017\t \t9,546\t5,479\t4,067', '2016\t \t9,222\t5,418\t3,804', '2015\t \t8,859\t5,363\t3,496', '2014\t \t8,203\t5,099\t3,104', '2013\t \t7,766\t4,861\t2,905', '2012\t \t7,091\t4,520\t2,571', '2011\t \t6,953\t4,526\t2,427', '2010\t \t6,632\t4,509\t2,123', '2009\t \t5,929\t4,011\t1,918', '2008\t \t5,909\t4,080\t1,829']
我想要一个列名为year,data1,data2,data3的dataframe
谢谢。你知道吗
另一种方法是不使用regex(但不如Romans answer整洁),使用列表理解清理数据,然后放入dict中,从中创建数据帧:
输出:
}
编辑后:
编辑后输出:
编辑:
与以下内容大致相同:
==>
通过
re
模块和生成器表达式:假设我们每年都有数据。你知道吗
相关问题 更多 >
编程相关推荐