Python dict分为数据帧表示、列表和文本条目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
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]
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)