(author: Chen’s freebies)
1, error scenario:
import pandas as pd
dict = {'a':1,'b':2,'c':3}
data = pd.DataFrame(dict)
2, error reason:
The dictionary
is passed in directly with the nominal attribute value and index is required, that is, index is set when the DataFrame object is created.
3, solution:
Creating DataFrame objects with a
dictionary is a common requirement, but it can be written differently depending on the object form. Look at the code, the following four methods can correct this error, and produce the same correct results, which method to use according to your own needs.
import pandas as pd
#方法一:直接在创建DataFrame时设置index即可
dict = {'a':1,'b':2,'c':3}
data = pd.DataFrame(dict,index=[0])
print(data)
#方法二:通过from_dict函数将value为标称变量的字典转换为DataFrame对象
dict = {'a':1,'b':2,'c':3}
pd.DataFrame.from_dict(dict,orient='index').T
print(data)
#方法三:输入字典时不要让Value为标称属性,把Value转换为list对象再传入即可
dict = {'a':[1],'b':[2],'c':[3]}
data = pd.DataFrame(dict)
print(data)
#方法四:直接将key和value取出来,都转换成list对象
dict = {'a':1,'b':2,'c':3}
pd.DataFrame(list(dict.items()))
print(data)
div>
Read More:
- Index error: invalid index to scalar variable
- [Solved] Es delete all the data in the index without deleting the index structure, including curl deletion
- SQL Union, union all usage and common errors and Solutions
- If you open the store in iTunes, you will be prompted to solve the 310 error
- Methods of modifying index and columns names by dataframe in pandas
- On set in pandas_ Index and reset_ Usage of index
- nbconvert failed: xelatex not found on PATH, if you have not installed xelatex you may need to do so
- Reintex index of pandas
- [antdv: DatePicker] `value` provides invalidate moment time. If you want set empty value, use `null`
- [Solved] RuntimeError: Expected object of scalar type Float but got scalar type Double for argument #2 ‘mat1‘
- Three solutions to the appearance of notice: undefined index in PHP
- Can’t find Python executable “D:\python3\python.exe”, you can set the PYTHON env variable.
- What to do if you repeatedly format a cluster
- You need to resolve your current index first
- 【Bug-python】IndexError: list index out of range
- Python3-ValueError:not enough values to unpack (expected 2, got 0)
- Linux common problems and Solutions
- An error occurs when pymysql uses% d to pass in parameters
- Common errors and solutions in MapReduce stage
- Common problems and solutions under Linux