Input: my_ dict = {‘i’: 1, ‘love’: 2, ‘you’: 3}
Expected output: my_ df
0
i 1
love 2
you 3
If the key and value in the dictionary are one-to-one, enter my directly_ df = pd.DataFrame (my_ “Value error: if using all scalar values, you must pass an index”.
The solution is as follows:
1. Specifies the index of the dictionary when using the dataframe function
import pandas as pd
my_dict = {'i': 1, 'love': 2, 'you': 3}
my_df = pd.DataFrame(my_dict,index=[0]).T
print(my_df)
2. Convert dictionary dict to list and transfer it to dataframe
import pandas as pd
my_dict = {'i': 1, 'love': 2, 'you': 3}
my_list = [my_dict]
my_df = pd.DataFrame(my_list).T
print(my_df)
3. Use DataFrame.from_ Dict function
For specific parameters, please refer to the official website: https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.from_ dict.html
import pandas as pd
my_dict = {'i': 1, 'love': 2, 'you': 3}
my_df = pd.DataFrame.from_dict(my_dict, orient='index')
print(my_df)
Output results
0
i 1
love 2
you 3
Read More:
- Summary of three methods for pandas to convert dict into dataframe
- Methods of modifying index and columns names by dataframe in pandas
- In pandas, dataframe and np.array The mutual transformation between the two
- Converting string object into datetime type in pandas
- Converting H264 bit stream to. MP4 format by ffmpeg
- Attributeerror: ‘dataframe’ object has no attribute ‘IX’ error
- Pandas multi column pandas.core.indexing . indexingerror: too many indexers error
- Dataframe and np.array The mutual transformation of
- Dataframe groupby custom aggregate function
- Pandas generates new columns through LOC
- Reintex index of pandas
- Python 3 error typeerror: ‘dict’_ keys‘ object is not subscriptable
- Pandas get column name
- Common attributes and methods of list and map in Dar
- [Python] pandas Library pd.to_ Parameter arrangement and example of Excel operation writing into excel file
- Pandas memory error
- Python data analysis dataframe converts dates to weeks
- Error converting data type nvarchar to datetime
- On set in pandas_ Index and reset_ Usage of index
- Converting PDF file to JPG image in Ubuntu