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!
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
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
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
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})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With