2

I have the following snippet of the pandas dataframe 'GDP', where the column labels are floats.

3        2013.0        2014.0        2015.0  
4           NaN           NaN           NaN  
5  3.127550e+09           NaN           NaN  
6  1.973134e+10  1.999032e+10  2.029415e+10  
7  9.626143e+10  1.008863e+11  1.039106e+11  
8  1.254247e+10  1.279331e+10  1.312082e+10  

I have tried to convert these float labels to integers with the following but did not succeed:

GDP.columns = GDP.columns.astype(int)

I keep getting the error: TypeError: Cannot cast Index to dtype

I then want to convert this to a string (like the following):

years = np.arange(2006, 2016).astype(str)
GDP = GDP[np.append(['Country'],years)]

Thus, my overall dataframe should look something like this: enter image description here

Could anyone give me a helping hand?

print(GDP.columns) looks like this:

Index([       'Country',   'Country Code', 'Indicator Name', 'Indicator Code',
                 1960.0,           1961.0,           1962.0,           1963.0,
                 1964.0,           1965.0,           1966.0,           1967.0,
                 1968.0,           1969.0,           1970.0,           1971.0,
                 1972.0,           1973.0,           1974.0,           1975.0,
                 1976.0,           1977.0,           1978.0,           1979.0,
                 1980.0,           1981.0,           1982.0,           1983.0,
                 1984.0,           1985.0,           1986.0,           1987.0,
                 1988.0,           1989.0,           1990.0,           1991.0,
                 1992.0,           1993.0,           1994.0,           1995.0,
                 1996.0,           1997.0,           1998.0,           1999.0,
                 2000.0,           2001.0,           2002.0,           2003.0,
                 2004.0,           2005.0,           2006.0,           2007.0,
                 2008.0,           2009.0,           2010.0,           2011.0,
                 2012.0,           2013.0,           2014.0,           2015.0],
      dtype='object', name=3)
4
  • What is print (Reducedset.columns) ? Commented May 16, 2020 at 11:57
  • @jezrael I have ammended the code above to show print(GDP.columns) Commented May 16, 2020 at 12:01
  • GDP.columns gives you the column names. Try GDP.astype(int) Commented May 16, 2020 at 12:04
  • 1
    @formicaman thanks! the solution below works! Commented May 16, 2020 at 12:09

2 Answers 2

2

Convert only numeric columns, here it means all columns after first 4. column and join together:

GDP.columns = GDP.columns[:4].tolist() + GDP.columns[4:].astype(int).astype(str).tolist()

And then:

years = np.arange(2006, 2016).astype(str)
GDP = GDP[np.append(['Country'],years)]
Sign up to request clarification or add additional context in comments.

7 Comments

thanks so much! In python should the data type of every column label be the same?
@Caledonian26 - I think no, each columns should be different type.
@Caledonian26 - But if need convert all columns togetehr, is necessary same type.
Yes I then converted: GDP.columns = GDP.columns.astype(str) and then continued with my formula and it worked nicely!
see my answer overall below!
|
0
GDP.columns = GDP.columns.map(int)

If you have numeric column labels mixed in with string labels:

for ind, col in enumerate(df.columns):
    if col.isdigit():
        df.columns.values[ind] = int(col)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.