[Python] Code Example 4 - Manipulating DataFrame (Add column)




[Source Code]

# Import cars data
import pandas as pd
cars = pd.read_csv('cars.csv', index_col = 0)

# Use .apply(str.upper)
cars["COUNTRY"] = cars["country"].apply(str.upper)

print(cars)


[Output]==========================================

<script.py> output:
         cars_per_cap        country  drives_right        COUNTRY
    US            809  United States          True  UNITED STATES
    AUS           731      Australia         False      AUSTRALIA
    JAP           588          Japan         False          JAPAN
    IN             18          India         False          INDIA
    RU            200         Russia          True         RUSSIA
    MOR            70        Morocco          True        MOROCCO

    EG             45          Egypt          True          EGYPT


[Reference]========================================

In [3]: cars
Out[3]: 
     cars_per_cap        country  drives_right
US            809  United States          True
AUS           731      Australia         False
JAP           588          Japan         False
IN             18          India         False
RU            200         Russia          True
MOR            70        Morocco          True

EG             45          Egypt          True

Comments

Popular posts from this blog

[Python] Code Example 1 - Dictionary

[Python] Code Example 2 - CSV to DataFrame