Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将长度可变的元组列表转换为数据帧_Python_Python 2.7_Pandas - Fatal编程技术网

Python 将长度可变的元组列表转换为数据帧

Python 将长度可变的元组列表转换为数据帧,python,python-2.7,pandas,Python,Python 2.7,Pandas,我有这个: [['COMPANY:', [('U S News & World Report Inc', 63)]], ['ORGANIZATION:', [('Ashoka', 0), ('Innovators For The Public', 91), ('Us Environmental Protection Agency', 55)]]] 我希望它成为一个熊猫数据帧,如: NAME ORGS

我有这个:

[['COMPANY:', [('U S News & World Report Inc', 63)]],
 ['ORGANIZATION:',
  [('Ashoka', 0),
   ('Innovators For The Public', 91),
   ('Us Environmental  Protection Agency', 55)]]]
我希望它成为一个熊猫数据帧,如:

NAME           ORGS                            PERCENT
Company        US News & World Report          63
Organization   Ashoka                          0
Organization   US Environmental Protection     55
结果:

           NAME                                 ORGS  PERCENT
0       Company          U S News & World Report Inc       63
1  Organization                               Ashoka        0
2  Organization            Innovators For The Public       91
3  Organization  Us Environmental  Protection Agency       55
结果:

           NAME                                 ORGS  PERCENT
0       Company          U S News & World Report Inc       63
1  Organization                               Ashoka        0
2  Organization            Innovators For The Public       91
3  Organization  Us Environmental  Protection Agency       55
结果:

           NAME                                 ORGS  PERCENT
0       Company          U S News & World Report Inc       63
1  Organization                               Ashoka        0
2  Organization            Innovators For The Public       91
3  Organization  Us Environmental  Protection Agency       55
结果:

           NAME                                 ORGS  PERCENT
0       Company          U S News & World Report Inc       63
1  Organization                               Ashoka        0
2  Organization            Innovators For The Public       91
3  Organization  Us Environmental  Protection Agency       55

这是一种您应该研究的
from_dict
read方法,适用于这种情况,只需将列表转换为字典即可:

L=[['COMPANY:', [('U S News & World Report Inc', 63)]],
 ['ORGANIZATION:',
  [('Ashoka', 0),
   ('Innovators For The Public', 91),
   ('Us Environmental  Protection Agency', 55)]]]

In [160]:
df=pd.DataFrame.from_dict(dict(L), orient="index").stack().reset_index(level=0)
df['Name']=df[0].apply(lambda x: x[0])
df['Val']=df[0].apply(lambda x: x[1])
df['Type']=df.level_0.str.slice(stop=-1)
df.__delitem__(0)
df.__delitem__('level_0')

In [161]:
print df
                                  Name  Val          Type
0                               Ashoka    0  ORGANIZATION
1            Innovators For The Public   91  ORGANIZATION
2  Us Environmental  Protection Agency   55  ORGANIZATION
0          U S News & World Report Inc   63       COMPANY

这是一种您应该研究的
from_dict
read方法,适用于这种情况,只需将列表转换为字典即可:

L=[['COMPANY:', [('U S News & World Report Inc', 63)]],
 ['ORGANIZATION:',
  [('Ashoka', 0),
   ('Innovators For The Public', 91),
   ('Us Environmental  Protection Agency', 55)]]]

In [160]:
df=pd.DataFrame.from_dict(dict(L), orient="index").stack().reset_index(level=0)
df['Name']=df[0].apply(lambda x: x[0])
df['Val']=df[0].apply(lambda x: x[1])
df['Type']=df.level_0.str.slice(stop=-1)
df.__delitem__(0)
df.__delitem__('level_0')

In [161]:
print df
                                  Name  Val          Type
0                               Ashoka    0  ORGANIZATION
1            Innovators For The Public   91  ORGANIZATION
2  Us Environmental  Protection Agency   55  ORGANIZATION
0          U S News & World Report Inc   63       COMPANY

这是一种您应该研究的
from_dict
read方法,适用于这种情况,只需将列表转换为字典即可:

L=[['COMPANY:', [('U S News & World Report Inc', 63)]],
 ['ORGANIZATION:',
  [('Ashoka', 0),
   ('Innovators For The Public', 91),
   ('Us Environmental  Protection Agency', 55)]]]

In [160]:
df=pd.DataFrame.from_dict(dict(L), orient="index").stack().reset_index(level=0)
df['Name']=df[0].apply(lambda x: x[0])
df['Val']=df[0].apply(lambda x: x[1])
df['Type']=df.level_0.str.slice(stop=-1)
df.__delitem__(0)
df.__delitem__('level_0')

In [161]:
print df
                                  Name  Val          Type
0                               Ashoka    0  ORGANIZATION
1            Innovators For The Public   91  ORGANIZATION
2  Us Environmental  Protection Agency   55  ORGANIZATION
0          U S News & World Report Inc   63       COMPANY

这是一种您应该研究的
from_dict
read方法,适用于这种情况,只需将列表转换为字典即可:

L=[['COMPANY:', [('U S News & World Report Inc', 63)]],
 ['ORGANIZATION:',
  [('Ashoka', 0),
   ('Innovators For The Public', 91),
   ('Us Environmental  Protection Agency', 55)]]]

In [160]:
df=pd.DataFrame.from_dict(dict(L), orient="index").stack().reset_index(level=0)
df['Name']=df[0].apply(lambda x: x[0])
df['Val']=df[0].apply(lambda x: x[1])
df['Type']=df.level_0.str.slice(stop=-1)
df.__delitem__(0)
df.__delitem__('level_0')

In [161]:
print df
                                  Name  Val          Type
0                               Ashoka    0  ORGANIZATION
1            Innovators For The Public   91  ORGANIZATION
2  Us Environmental  Protection Agency   55  ORGANIZATION
0          U S News & World Report Inc   63       COMPANY

嗯,我喜欢有一个具体的方法来解决这个问题,但是我不喜欢ti比使用for循环更复杂。不过,我将进一步研究这种方法。谢谢嗯,我喜欢有一个具体的方法来解决这个问题,但是我不喜欢ti比使用for循环更复杂。不过,我将进一步研究这种方法。谢谢嗯,我喜欢有一个具体的方法来解决这个问题,但是我不喜欢ti比使用for循环更复杂。不过,我将进一步研究这种方法。谢谢嗯,我喜欢有一个具体的方法来解决这个问题,但是我不喜欢ti比使用for循环更复杂。不过,我将进一步研究这种方法。谢谢