Reputation: 93
I am trying to concatenate a list and a dataframe in Python, by doing so I receive the error below:
error cannot concatenate object of type '<class 'list'>'; only Series and DataFrame objs are valid
.
The first df looks like this:
assets = ['ABN.AS','ACCEL.AS','ADYEN.AS','AGN.AS']
Type(assets)= list
The second df looks like this:
Curr=
ABN.AS ACCEL.AS ADYEN.AS AGN.AS Idx
1 EUR EUR EUR EUR currency
type(curr)=pandas.core.frame.DataFrame
Above, the number 1 is just redundant information, it automatically appears if I run the code to scrape the currency of the ticker. Python does not see the number 1 as separate column (while running print(curr)
I have removed the last column, which is not relevant.
curr = curr.drop(['idx'],axis =1)
I can't delete the header row, I tried this: curr.columns = range(curr.shape[1])
The df looks like this now, I can't remove the annoying 2, which is redundant.
0 1 2 3 4
2 EUR EUR EUR EUR EUR
The goal is to create a new df (dictionary), which have to look like this:
assets_result={'ABN.AS':'EUR','ACCEL.AS':'EUR','ADYEN.AS':'EUR','AGN.AS':'EUR'}
Type(assets_result)= dict
I have tried to do this, which resulted in the above mentioned error:
result = pd.concat([assets, curr])
Upvotes: 2
Views: 113
Reputation: 260790
You can simply do:
curr.drop(['idx'], axis=1).iloc[0].to_dict()
or:
curr[assets].iloc[0].to_dict()
Upvotes: 2