In [1]:
#Create DataFrame
import pandas as pd
import numpy as np
from pandas import DataFrame, Series
df = DataFrame(
    {'integer':[1,2,3,6,7,23,8,3],
     'float':[2,3.4,5,6,2,4.7,4,8],
     'string':['saya',None,'aku','cinta','kamu','a','b','jika']}
)
In [2]:
#Show DataFrame
df
Out[2]:
float integer string
0 2.0 1 saya
1 3.4 2 None
2 5.0 3 aku
3 6.0 6 cinta
4 2.0 7 kamu
5 4.7 23 a
6 4.0 8 b
7 8.0 3 jika
In [3]:
#Drop Na Value from DataFrame and then change the value using map
df['string'].dropna().map(lambda x : 'rischan_' + x)
Out[3]:
0     rischan_saya
2      rischan_aku
3    rischan_cinta
4     rischan_kamu
5        rischan_a
6        rischan_b
7     rischan_jika
Name: string, dtype: object
In [5]:
#Apply function for all rows in dataframe
df.ix[:,['float','integer']].apply(np.sqrt)
Out[5]:
float integer
0 1.414214 1.000000
1 1.843909 1.414214
2 2.236068 1.732051
3 2.449490 2.449490
4 1.414214 2.645751
5 2.167948 4.795832
6 2.000000 2.828427
7 2.828427 1.732051
In [6]:
#Apply map function example
def applymap_function(x):
    if type(x) is str:
        return 'applymap_' +x
    elif x:
        return 100 * x
    else:
        return

#DataFrame after function excecution
df.applymap(applymap_function)
Out[6]:
float integer string
0 200 100 applymap_saya
1 340 200 None
2 500 300 applymap_aku
3 600 600 applymap_cinta
4 200 700 applymap_kamu
5 470 2300 applymap_a
6 400 800 applymap_b
7 800 300 applymap_jika
In [8]:
#Columns opereration in DataFrame
df["total"] = df["float"]+df["integer"]
df
Out[8]:
float integer string total
0 2.0 1 saya 3.0
1 3.4 2 None 5.4
2 5.0 3 aku 8.0
3 6.0 6 cinta 12.0
4 2.0 7 kamu 9.0
5 4.7 23 a 27.7
6 4.0 8 b 12.0
7 8.0 3 jika 11.0
In [9]:
#String operation in DataFrame
df["upper"] = df["string"].str.upper()
df
Out[9]:
float integer string total upper
0 2.0 1 saya 3.0 SAYA
1 3.4 2 None 5.4 None
2 5.0 3 aku 8.0 AKU
3 6.0 6 cinta 12.0 CINTA
4 2.0 7 kamu 9.0 KAMU
5 4.7 23 a 27.7 A
6 4.0 8 b 12.0 B
7 8.0 3 jika 11.0 JIKA