1. Error scenarios:
import pandas as pd
dict = {'a':1,'b':2,'c':3}
data = pd.DataFrame(dict)
2. Error reason:
When passing in the dictionary with nominal attribute value directly, you need to write index, that is, you need to set index when creating the dataframe object.
3. Solution:
It is a common requirement to create dataframe objects through dictionaries, but there may be different writing methods for different object forms. Looking at the code, the following four methods can correct this error and produce the same correct results. Just choose which method to use according to your own needs.
import pandas as pd
#Method 1: Directly set the index when creating the DataFrame
dict = {'a':1,'b':2,'c':3}
data = pd.DataFrame(dict,index=[0])
print(data)
#Method 2: Convert the dictionary with value as nominal variable to DataFrame object by from_dict function
dict = {'a':1,'b':2,'c':3}
pd.DataFrame.from_dict(dict,orient='index').T
print(data)
#Method 3: When entering the dictionary, do not let the Value be the nominal property, convert the Value to a list object and then pass it in.
dict = {'a':[1],'b':[2],'c':[3]}
data = pd.DataFrame(dict)
print(data)
#Method 4: directly take out the key and value, are converted to list objects
dict = {'a':1,'b':2,'c':3}
pd.DataFrame(list(dict.items()))
print(data)
Read More:
- Python Valueerror: cannot index with vector containing Na / Nan values
- [resolution] str.contains() problem] valueerror: cannot index with vector containing Na/Nan values
- [Solved] yolo Error: IndexError: invalid index to scalar variable.
- [How to Fix]RuntimeError: Python is not installed as a framework, If you are using (Ana)Conda
- Python 3.X error: valueerror: data type must provide an itemsize
- [Solved] PyInstaller Error: ValueError: too many values to unpack
- [Solved] ValueError: row index was 65536, not allowed by .xls format
- Pandas Error: ValueError: setting an array element with a sequence.
- Autograd error in Python: runtimeerror: grad can be implicitly created only for scalar outputs
- Solve the error of panda index unalignable Boolean series provided as indexer
- [Solved] python Error: ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.
- XGBoost Common Errors and Their Solutions
- Python TypeError: not all arguments converted during string formatting [Solved]
- Pandas ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.an
- How to Solve Python Xlwt ValueError: More than 4094 XFs (styles)
- Python ValueError: only 2 non-keyword arguments accepted
- [Solved] opencv-python: recipe for target ‘modules/python3/CMakeFiles/opencv_python3.dir/all‘ failed
- Python traverses all files under the specified path and retrieves them according to the time interval
- Python recursively traverses all files in the directory to find the specified file
- [Solved] ValueError: only one element tensors can be converted to Python scalars