I have a list where a is 806 in length. I want to import to a dataframe where the first item in the list is the column name. My code is:
import pandas as pd
b = pd.DataFrame.from_records(a[1:],columns=[a[0]])
this gives me an error of AssertionError: 1 columns passed, passed data had 22 columns while clearly i have only one column.
I've tried a representation of the code and it works. So I'm not sure what is going on here. Here is a representation of the code:
import pandas as pd
arr= ['title', 'a','b','','','']
arr= filter(None, arr)
b = pd.DataFrame.from_records(arr[1:],columns=[arr[0]] )
Must be something wrong with my list? I printed out a and it looks fine, like a regular list. I have pasted the printed output of a and placed that as the variable list, and it gives me the same error AssertionError: 1 columns passed, passed data had 22 columns. Seems like something wrong with my list. What else can I do to debug?
Edit (based on DSM suggestion):
import pandas as pd
arr=['Title', '000660.ks']
b = pd.DataFrame.from_records(arr[1:],columns=[arr[0]] )
This gives AssertionError: 1 columns passed, passed data had 8 columns
解决方案
Instead of using from_records you want to use the default DataFrame constructor.
from_records expects a list of something iterable, so for example, the string '0006660.ks' is being read in as ('0','0',... ,'s') which is why you are getting an error about 8 columns in the data.
b = pd.DataFrame(a[1:], columns=[a[0]])