Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ValueError: 2 columns passed, passed data had 1 columns

I have a list with name of organizations like this:

name = ['ALPHABET INC', 'AMAZON COM INC', 'APPLE INC',....]

and another list of cu values like this:

cu = ['02079K305', '023135106', '037833100',....]

When i'm trying to convert it to dataframe it's giving me error message saying, "ValueError: 2 columns passed, passed data had 1 columns"

My code to convert list ot dataframe:

df = pd.DataFrame([name, cu], columns=['name of issuer', 'cusip'])

Where am i going wrong? Thanks in advance!

like image 981
Ishan Pradhan Avatar asked Mar 19 '20 06:03

Ishan Pradhan


4 Answers

I think simpliest is create dictionaries:

df = pd.DataFrame({'name of issuer': name, 'cusip':cu})  

Your solution is possible with zip, in last version of pandas should be omit list:

df = pd.DataFrame(list(zip(name, cu)), columns=['name of issuer', 'cusip'])

print (df)
   name of issuer      cusip
0    ALPHABET INC  02079K305
1  AMAZON COM INC  023135106
2       APPLE INC  037833100
like image 67
jezrael Avatar answered Nov 12 '22 04:11

jezrael


Use zip

pd.DataFrame(zip(name, cu), columns=['name of issuer', 'cusip'])

ex:

>>> df = pd.DataFrame(zip(name, cu), columns=['name of issuer', 'cusip'])
>>> df
   name of issuer      cusip
0    ALPHABET INC  02079K305
1  AMAZON COM INC  023135106
2       APPLE INC  037833100
like image 4
Dishin H Goyani Avatar answered Nov 12 '22 02:11

Dishin H Goyani


Your structure of the list puts the data into rows rather than columns.

df = pd.DataFrame([name, cu]).T
df.columns = ['name of issuer', 'cusip']

>>> df
   name of issuer      cusip
0    ALPHABET INC  02079K305
1  AMAZON COM INC  023135106
2       APPLE INC  037833100
like image 2
Vishnudev Avatar answered Nov 12 '22 02:11

Vishnudev


Right now, panda thinks the value you are passing is one large list. One way to change this is to pass in a dictionary as data with column names as keys and lists as values.

df = pd.DataFrame({'name of issuer' : name, 'cusip' : cu})

like image 1
Rahul Nair Avatar answered Nov 12 '22 02:11

Rahul Nair