Following a pd.melt, I cannot drop a column in my df. I realised this is because it is not a string, here is the df:
{'country': {0: 'Total_MIC_Debt', 1: 'Total_MIC_Debt', 2: 'Total_MIC_Debt', 3: 'Total_MIC_Debt', 4: 'Total_MIC_Debt'}, None: {0: 'ExternalDebtStocks', 1: 'ExternalDebtStocks', 2: 'ExternalDebtStocks', 3: 'ExternalDebtStocks', 4: 'ExternalDebtStocks'}, 'year': {0: '1970', 1: '1971', 2: '1972', 3: '1973', 4: '1974'}, 'ExternalDebtStocks': {0: 60777211151.7, 1: 70984695463.1, 2: 82425188476.20001, 3: 98876727143.09999, 4: 122523812877.09999}}
From which if following a df.head() to get a look at it, if I do,
print(df.columns)
I get:
Index(['country', None, 'year', 'ExternalDebtStocks'], dtype='object')
From which I can see the 1st item in the list is None, not in quotes, from which can see if do:
df.columns.map(type)
output:
Index([<class 'str'>, <class 'NoneType'>, <class 'str'>, <class 'str'>], dtype='object')
It is NoneType.
To convert it to string like the rest of them I tried the following from answers I found elsewhere on Stack Overflow
I tried:
df = df.astype({None : 'string'})
output:
ValueError: entry not a 2- or 3- tuple
I then tried, fairly sure it also wouldn't work:
df_ = df.astype({'None' : 'string'})
output:
ValueError: entry not a 2- or 3- tuple
I then tried another answer I found:
df.columns = df.columns.astype(str)
output:
AttributeError: 'Index' object has no attribute 'columns'
Which I find confusing, as to how my object has no columns when
print(df.columns)
Produces something. I also tried the more obscure:
df.columns = [str(col) for col in df.columns]
output:
AttributeError: 'Index' object has no attribute 'columns'
Any ideas?
All I want is to convert the column name None from NoneType to string.
Many Thanks