如何在Python中将混合数据类型的列表转换为数据帧

2024-05-15 04:02:55 发布

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

我有一个混合数据类型列表,如下所示:

list = [['3D prototypes',
  'Can print large objects',
  'Autodesk Maya/Mudbox',
  '3D Studio'],
 ['We can produce ultra high resolution 3D prints in multiple materials.',
  'The quality of our prints beats MakerBot, Form 1, or any other either         
  powder based or printers using PLA, ABS, Wax or Resin. This printer has  
  the highest resolution and a very large build size. It prints fully  
  functional moving parts like a chain or an engine right out of the 
  printer.',
  'The printer is loaded with DurusWhite.',
  'Inquire to change the material. There is a $30 surcharge for material   
  switch.',
  "Also please mention your creation's dimensions in mm and if you need  
  expedite delivery.",
  "Printer's Net build size:",
  '294 x 192 x 148.6 mm (11.57 x 7.55 x 5.85 in.)',
  'The Objet30 features four Rigid Opaque materials and one material that  
  mimics polypropylene. The Vero family of materials all feature dimensional  
  stability and high-detail visualization, and are designed to simulate 
  plastics that closely resemble the end product.',
  'PolyJet based printers have a different way of working. These  
  technologies deliver the highest quality and precision unmatched by the 
  competition. These type of printers are ideal for professionals, for uses 
  ranging from casting jewelry to device prototyping.',
  'Rigid opaque white (VeroWhitePlus)',
  'Rigid opaque black (VeroBlackPlus )',
  'Rigid opaque blue (VeroBlue)',
  'Rigid opaque gray (VeroGray)',
  'Polypropylene-like material (DurusWhite) for snap fit applications'],
  'Hub can print invoices',
  'postal service',
  'Mar 2015',
  'Within the hour i',
  [u'40.7134', u'-74.0069'],
  '4',
  ['Customer JAMES reviewed Sun, 2015-04-19 05:17:        Awesome print!  
  Good quality, relatively fast shipping, and very responsive to my  
  questions; would certainly recommend this hub. ',
  'Hub XSENIO replied 2 days 16 hours ago:        Thanks James! ',
  'Customer Sara reviewed Sun, 2015-04-19 00:10:        Thank you for going  
  out of your way to get this to us in time for our shoot.  ',
 'Hub XSENIO replied 2 days 16 hours ago:        Thanks ! ',
 'Customer Aaron reviewed Sat, 2015-04-18 02:36:        Great service ',
 'Hub XSENIO replied 2 days 16 hours ago:        Thanks! ',
 "Customer Arnoldas reviewed Mon, 2015-03-23 19:47:        Xsenio's Hub was  
 able to produce an excellent quality print , was quick and reliable. 
 Awesome printing experience!  "]]

它的混合数据类型如下所示

^{pr2}$

但当我使用

 pd.DataFrame(list)

它表明

 TypeError: Expected list, got str

谁能告诉我这有什么问题吗?是否必须将列表中的所有项从字符串转换为列表?在

谢谢


Tags: orandofthetoinforcustomer
1条回答
网友
1楼 · 发布于 2024-05-15 04:02:55

似乎应该将列表转换为numpy arraydict

from pandas import DataFrame
import numpy
lst = numpy.array([['3D prototypes',
        'Can print large objects',
        'Autodesk Maya/Mudbox',
        '3D Studio'],
       ['We can produce ultra high resolution 3D prints in multiple materials.',
        '''The quality of our prints beats MakerBot, Form 1, or any other either
        powder based or printers using PLA, ABS, Wax or Resin. This printer has
        the highest resolution and a very large build size. It prints fully
        functional moving parts like a chain or an engine right out of the
        printer.''',
        'The printer is loaded with DurusWhite.',
        '''Inquire to change the material. There is a $30 surcharge for material
        switch.''',
        '''Also please mention your creation's dimensions in mm and if you need
        expedite delivery.''',
        "Printer's Net build size:",
        '294 x 192 x 148.6 mm (11.57 x 7.55 x 5.85 in.)',
        '''The Objet30 features four Rigid Opaque materials and one material that
        mimics polypropylene. The Vero family of materials all feature dimensional
        stability and high-detail visualization, and are designed to simulate
        plastics that closely resemble the end product.''',
        '''PolyJet based printers have a different way of working. These
        technologies deliver the highest quality and precision unmatched by the
        competition. These type of printers are ideal for professionals, for uses
        ranging from casting jewelry to device prototyping.''',
        'Rigid opaque white (VeroWhitePlus)',
        'Rigid opaque black (VeroBlackPlus )',
        'Rigid opaque blue (VeroBlue)',
        'Rigid opaque gray (VeroGray)',
        'Polypropylene-like material (DurusWhite) for snap fit applications'],
       'Hub can print invoices',
       'postal service',
       'Mar 2015',
       'Within the hour i',
       [u'40.7134', u'-74.0069'],
       '4',
       ['''Customer JAMES reviewed Sun, 2015-04-19 05:17:        Awesome print!
       Good quality, relatively fast shipping, and very responsive to my
       questions; would certainly recommend this hub. ''',
        'Hub XSENIO replied 2 days 16 hours ago:        Thanks James! ',
        '''Customer Sara reviewed Sun, 2015-04-19 00:10:        Thank you for going
        out of your way to get this to us in time for our shoot.  ''',
        'Hub XSENIO replied 2 days 16 hours ago:        Thanks ! ',
        'Customer Aaron reviewed Sat, 2015-04-18 02:36:        Great service ',
        'Hub XSENIO replied 2 days 16 hours ago:        Thanks! ',
        '''Customer Arnoldas reviewed Mon, 2015-03-23 19:47:        Xsenio's Hub was
        able to produce an excellent quality print , was quick and reliable.
        Awesome printing experience!  ''']])

df = DataFrame(lst)
print df

以上图片

^{pr2}$

doc声明data参数应该是numpy数组或dict:http://pandas.pydata.org/pandas-docs/dev/generated/pandas.DataFrame.html

PS:我还冒昧地将多行字符串用三引号括起来

相关问题 更多 >

    热门问题