Environment and Prerequisite

  • Python
  • Pandas


Usage and Example

  • Use apply in DataFrame
  • Add function to apply as argument
  • Use axis=1 to use per row
  • Parameter in function represents each row in DataFrame
import pandas as pd

# make 20 rows of data
data = {'col1': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20],
        'col2': ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T'],
        'col3': [True, False, True, False, True, False, True, False, True, False, True, False, True, False, True, False, True, False, True, False]}

# make dataframe
df = pd.DataFrame(data)

# define function which will use for apply
# access column by column name
def combine_values_by_column_name(row):
    return str(row['col1']) + row['col2'] + str(row['col3'])

# define function which will use for apply
# access column by index
def combine_values_by_column_name(row):
    return str(row[0]) + row[1] + str(row[2])

# use apply
# watch out for axis value
df['new_col_by_name'] = df.apply(combine_values, axis=1)
df['new_col_by_index'] = df.apply(combine_values, axis=1)

# show result
df.head()
  • Result
  col1 col2 col3 new_col_by_name new_col_by_index
0 1 A True 1ATrue 1ATrue
1 2 B False 2BFalse 2BFalse
2 3 C True 3CTrue 3CTrue
3 4 D False 4DFalse 4DFalse
4 5 E True 5ETrue 5ETrue


Reference


환경

  • Java


사용

  • instanceof 사용
if (parentObj instanceof ChildClass) {
    System.out.println("This is a ChildClass object");
}


예시

Parent Class

  • Animal.java
public abstract class Animal {
    public abstract void action();
}

Child Classes

  • Bird.java
public class Bird extends Animal{
    @Override
    public void action() {
        System.out.println("bird!");
    }
}
  • Cat.java
public class Cat extends Animal{
    @Override
    public void action() {
        System.out.println("cat!");
    }
}
  • Dog.java
public class Dog extends Animal{
    @Override
    public void action() {
        System.out.println("dog!");
    }
}

Main

  • Main.java
public class Main {
    public static void main(String[] args) {
        Animal bird = new Bird();
        Animal cat = new Cat();
        Animal dog = new Dog();

        testMethod(dog);
        testMethod(cat);
        testMethod(bird);
    }

    private static void testMethod(Animal animal) {
        if (animal instanceof Bird){
            System.out.println("This is bird instance");
        } else if (animal instanceof  Cat) {
            System.out.println("This is cat instance");
        } else if (animal instanceof Dog) {
            System.out.println("This is dog instance");
        } else {
            System.out.println("Nothing");
        }
        animal.action();
    }
}

결과

> Task :Main.main()
This is dog instance
dog!
This is cat instance
cat!
This is bird instance
bird!


참고자료


Environment and Prerequisite

  • Java


Usage

  • Use instanceof
if (parentObj instanceof ChildClass) {
    System.out.println("This is a ChildClass object");
}


Example

Parent Class

  • Animal.java
public abstract class Animal {
    public abstract void action();
}

Child Classes

  • Bird.java
public class Bird extends Animal{
    @Override
    public void action() {
        System.out.println("bird!");
    }
}
  • Cat.java
public class Cat extends Animal{
    @Override
    public void action() {
        System.out.println("cat!");
    }
}
  • Dog.java
public class Dog extends Animal{
    @Override
    public void action() {
        System.out.println("dog!");
    }
}

Main

  • Main.java
public class Main {
    public static void main(String[] args) {
        Animal bird = new Bird();
        Animal cat = new Cat();
        Animal dog = new Dog();

        testMethod(dog);
        testMethod(cat);
        testMethod(bird);
    }

    private static void testMethod(Animal animal) {
        if (animal instanceof Bird){
            System.out.println("This is bird instance");
        } else if (animal instanceof  Cat) {
            System.out.println("This is cat instance");
        } else if (animal instanceof Dog) {
            System.out.println("This is dog instance");
        } else {
            System.out.println("Nothing");
        }
        animal.action();
    }
}

Result

> Task :Main.main()
This is dog instance
dog!
This is cat instance
cat!
This is bird instance
bird!


Reference


환경

  • Python
  • Pandas


사용법

  • .astype() 사용
import pandas as pd

df = pd.DataFrame({'A': ['1', '2', '3'], 'B': [4, 5, 6]})

print(df.dtypes)

# convert column 'A' from string to int
df['A'] = df['A'].astype(int)

print(df.dtypes)


참고자료


Environment and Prerequisite

  • Python
  • Pandas


Usage

  • Use .astype()
import pandas as pd

df = pd.DataFrame({'A': ['1', '2', '3'], 'B': [4, 5, 6]})

print(df.dtypes)

# convert column 'A' from string to int
df['A'] = df['A'].astype(int)

print(df.dtypes)


Reference