目标:
我想生成一个虚拟数据帧来测试某些功能,但是我无法将数组传递到DataFrame中。
情况:
我想插入第一列:dates
,随后的列将是字符串或整数。
我的代码:
import pandas as pd
import numpy as np
col_names = ['Date', 'a', 'b', 'Dernier', 'Frequences', 'Total'] # 6 columns
data =[['2019-01-21',456,'dwfv84',23,74,261,4221],
['2019-02-10',123,'qwbe78',3,83,9251],
['2019-01-25',789,'adqw87',19,478,19195],
['2018-01-04',988,'afdi25',40,321,3753],
['2018-03-19',784,'asdf48',331,413,8551],
['2018-04-15',445,'asfv41',304,246,10215],
['2018-04-10',589,'sdqw88',309,80,19569],
['2018-05-20',741,'dsdg46',269,282,3108],
['2018-06-30',852,'cvgo87',228,261,5975],
['2019-01-19',963,'ewgs45',25,357,4405],
['2019-01-12',369,'fbbr54',32,197,1019],
['2019-01-18',258,'fwgs77',26,132,18100],
['2019-02-10',147,'jkyu87',3,32,8678],
['2019-02-05',753,'yukh20',8,132,19871]]
my_data= np.array(data)
datas = pd.DataFrame(data=my_data, columns=col_names)
错误消息:
ValueError:传递的项目数错误1,展示位置意味着6
ValueError:传递的值的形状为(1,14),索引表示(6,14)
答案 0 :(得分:1)
从第一行中删除了“ 74”
import pandas as pd
import numpy as np
col_names = ['Date', 'a', 'b', 'Dernier', 'Frequences', 'Total'] # 6 columns
data =[['2019-01-21',456,'dwfv84',23, 261,4221],
['2019-02-10',123,'qwbe78',3,83,9251],
['2019-01-25',789,'adqw87',19,478,19195],
['2018-01-04',988,'afdi25',40,321,3753],
['2018-03-19',784,'asdf48',331,413,8551],
['2018-04-15',445,'asfv41',304,246,10215],
['2018-04-10',589,'sdqw88',309,80,19569],
['2018-05-20',741,'dsdg46',269,282,3108],
['2018-06-30',852,'cvgo87',228,261,5975],
['2019-01-19',963,'ewgs45',25,357,4405],
['2019-01-12',369,'fbbr54',32,197,1019],
['2019-01-18',258,'fwgs77',26,132,18100],
['2019-02-10',147,'jkyu87',3,32,8678],
['2019-02-05',753,'yukh20',8,132,19871]]
my_data= np.array(data)
datas = pd.DataFrame(data=my_data, columns=col_names)