Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
87 views
in Technique[技术] by (71.8m points)

python - dict into dataframe representation, dict of list and text entries

I want to change a dict entry into a dataframe. Example:

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']
    }

The output I want is something like this:

        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]

If I do pd.DataFrame.from_dict(data1), I get this error

ValueError: arrays must all be same length

in which I fixed by

pd.DataFrame.from_dict(data1,orient='index').transpose()

However, two issues arose:

enter image description here

1st: the description is truncated. Only this portion of the text appears: "Python is an interpreted, high-level and genre..."

2nd: the output is split into two entries as in the image above.

How can I solve these issues?

question from:https://stackoverflow.com/questions/65887467/dict-into-dataframe-representation-dict-of-list-and-text-entries

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Convert the categories into list of list

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]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...