enter: 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, then directly entering my_df = pd.DataFrame(my_dict) will give an error “ValueError: If using all scalar values, you must pass an index”.
p>
solution:
1, use DataFrame to specify the dictionary index index
import pandas as pd
my_dict = {'i': 1, 'love': 2, 'you': 3}
my_df = pd.DataFrame(my_dict,index=[0]).T
print(my_df)
h3>
2. 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)
h3>
3, DataFrame. From_dict
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, DataFrame. From_dict
specific parameters can refer to the website: https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.from_dict.html p>
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
0
i 1
love 2
you 3
div>
Read More:
- Three methods of converting dict into dataframe by pandas
- Methods of modifying index and columns names by dataframe in pandas
- In pandas, dataframe and np.array The mutual transformation between the two
- Summary of Hadoop error handling methods
- Attributeerror: ‘dataframe’ object has no attribute ‘IX’ error
- Dataframe and np.array The mutual transformation of
- Pandas multi column pandas.core.indexing . indexingerror: too many indexers error
- Pandas generates new columns through LOC
- Dataframe groupby custom aggregate function
- Reintex index of pandas
- Pandas sort according to a column_ values)
- Pandas get column name
- Python data analysis dataframe converts dates to weeks
- Converting string object into datetime type in pandas
- Python 3 error typeerror: ‘dict’_ keys‘ object is not subscriptable
- Dataframe to numpy.ndarray Related issues of
- [Python] pandas Library pd.to_ Parameter arrangement and example of Excel operation writing into excel file
- Summary of Python deep learning packages
- 2021-11-08 error: could not convert ‘{0}‘ from ‘<brace-enclosed initializer list>‘ to
- How to convert audio to subtitle (text) with Python?