Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/352.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 dict分为数据帧表示、列表和文本条目dict_Python_Pandas_String_Dataframe - Fatal编程技术网

Python dict分为数据帧表示、列表和文本条目dict

Python dict分为数据帧表示、列表和文本条目dict,python,pandas,string,dataframe,Python,Pandas,String,Dataframe,我想将dict条目更改为数据帧。例如: data1 ={'Description': ['Python is an interpreted, high-level and general-purpose programming language. Python\'s design philosophy emphasizes code readability with its notable use of significant whitespace. Its language construct

我想将dict条目更改为数据帧。例如:

data1 ={'Description': ['Python is an interpreted, high-level and general-purpose programming language. Python\'s design philosophy emphasizes code readability with its notable use of significant whitespace. Its language constructs and object-oriented approach aim to help programmers write clear, logical code for small and large-scale projects.[28]'],
    'Site': ['Wikipedia'],
    'Categories': ['Python', 'Programming']
    }
我想要的输出如下:

        Description                                          Site            Categories
0       Python is an interpreted, high-level and             Wikipedia       [Python, Programming]
        general-purpose programming language. Python's 
        design philosophy emphasizes code readability 
        with its notable use of significant whitespace. 
        Its language constructs and object-oriented
        approach aim to help programmers write clear, 
        logical code for small and large-scale projects
         .[28]
如果我从dict(data1)执行pd.DataFrame,我会得到这个错误

ValueError:数组的长度必须相同

在里面我被

pd.DataFrame.from_dict(data1,orient='index').transpose()
然而,出现了两个问题:

第一:描述被截断了。只有这部分文字出现:“Python是一种解释性的、高层次的和体裁的…”

第二:输出被分成两个条目,如上图所示


如何解决这些问题?

您正在使用的
DataFrame
构造函数使用一个
dict
,其中键是
DataFrame
中列的名称,如果您希望列类别中第一行的值成为一个列表,那么对于每个键,您在列表中的该列中都有您想要的值。然后,与
'Categories'
关联的列表的第一个元素必须是列表。作为一个简单的例子,请考虑:

data1 = {'Site': ['Wikipedia'],
         'Categories': [ ['Python', 'Programming'] ]} # list[list[str]]
df = pd.DataFrame(data1)

将类别转换为列表的列表

data1 ={'Description': ['Python is an interpreted, high-level and general-purpose programming language. Python\'s design philosophy emphasizes code readability with its notable use of significant whitespace. Its language constructs and object-oriented approach aim to help programmers write clear, logical code for small and large-scale projects.[28]'],
'Site': ['Wikipedia'],
'Categories': [['Python', 'Programming']]
}


In[19]:  df
Out[19]: 
                                     Description  ...             Categories
0  Python is an interpreted, high-level and gener...  ...  [Python, Programming]

[1 rows x 3 columns]
  • 由于您只有1项,因此需要将其传递到任何可迭代对象(它是可用的变体之一)。例如
  • 描述没有被截断。显示只是一个预览。如果您通过
    frame[“description”]
    选择description字段,您将看到完整内容
  • data = [
        {
            "Description": "Python is an interpreted, high-level and general-purpose programming etc.",
            "Site": "Wikipedia",
            "Categories": ["Python", "Programming"]
        }
    ]
    frame = pd.DataFrame(data)