Home>

Add a key to the column
I want to put a COUNTRY key in cars, but I can't.

____________________________

Problem description

  

The call should give the same result: a column COUNTRY should be added to cars

My code

# Import cars data
import pandas as pd
cars = pd.read_csv ('cars.csv', index_col = 0)
# Use .apply (str.upper)
cars ["name_length"] = cars ["country"]. apply (str.upper)
print (cars)

Error text

Did you correctly define the variable cars? There is no key 'COUNTRY'.
  • Answer # 1

    If you want to put the keyCOUNTRYin

    dataframecars, you need to setcars ["COUNTRY"] =Socars ["COUNTRY"] = cars ["country"]. Apply (str.upper)will work.
    In the above code, key is specified asname_length.

    By the way, it is an error statement, but I don't think this is an error in Python itself.
    In the above code, the column namename_lengthwith thecountryvalue capitalized is inserted intocars, and an error occurs. There should be no.

Related articles

Trends