환경

  • Python
  • Pandas


사용법

  • pd.concat() 사용
import pandas as pd

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

# create a new row as a DataFrame
new_row = pd.DataFrame({'A': [4], 'B': [7]})

# concatenate the existing DataFrame and the new row
df = pd.concat([df, new_row], ignore_index=True)

df


참고자료


Environment and Prerequisite

  • Python
  • Pandas


Usage

  • Use pd.concat()
import pandas as pd

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

# create a new row as a DataFrame
new_row = pd.DataFrame({'A': [4], 'B': [7]})

# concatenate the existing DataFrame and the new row
df = pd.concat([df, new_row], ignore_index=True)

df


Reference


환경

  • Python
  • Pandas


사용법

  • 조건에 따라 기존 값들 변경하기
  • df.loc[] 사용
import pandas as pd

df = pd.DataFrame({'A': [1, 2, 3, 4], 'B': [5, 6, 7, 8], 'C': [9, 10, 11, 12]})

# modify values in column 'B' and 'C' based on a condition in column 'A'
df.loc[df['A'] < 3, ['B', 'C']] = 0

df
  • 조건을 사용해 새로운 컬럼 생성하기
  • np.where() 사용
import pandas as pd
import numpy as np

df = pd.DataFrame({'A': range(0, 30), 'B': range(30, 60)})

# make condition using & or |
cond_1 = (5 <= df['A']) & (df['A'] <= 10)
cond_2 = (15 <= df['A']) & (df['A'] <= 20)

print(np.where( cond_1 | cond_2))

# create a new column C based on values in column A and an if condition
df['A is in 5~10 or 15~20'] = np.where(cond_1 | cond_2, True, False)

df


참고자료


Environment and Prerequisite

  • Python
  • Pandas


Usage

  • Modify exist values depends on condition
  • Use df.loc[]
import pandas as pd

df = pd.DataFrame({'A': [1, 2, 3, 4], 'B': [5, 6, 7, 8], 'C': [9, 10, 11, 12]})

# modify values in column 'B' and 'C' based on a condition in column 'A'
df.loc[df['A'] < 3, ['B', 'C']] = 0

df
  • Make new column depends on condition
  • Use np.where()
import pandas as pd
import numpy as np

df = pd.DataFrame({'A': range(0, 30), 'B': range(30, 60)})

# make condition using & or |
cond_1 = (5 <= df['A']) & (df['A'] <= 10)
cond_2 = (15 <= df['A']) & (df['A'] <= 20)

print(np.where( cond_1 | cond_2))

# create a new column C based on values in column A and an if condition
df['A is in 5~10 or 15~20'] = np.where(cond_1 | cond_2, True, False)

df


Reference


환경

  • Python
  • Pandas


사용법

  • 아래 예시들이 다 동일한 결과를 만든다.
import pandas as pd

df = pd.DataFrame({'A': ["a", "b", "c"], 'B': [4, 5, 6]})

def modify_string(a):
    return a.upper() + "1"

def add_one(a):
    return a + 1

df['A'] = df['A'].apply(modify_string)
df['B'] = df['B'].apply(add_one)

df
import pandas as pd

df = pd.DataFrame({'A': ["a", "b", "c"], 'B': [4, 5, 6]})


df['A'] = df['A'].apply(lambda x: x.upper() + "1")
df['B'] = df['B'].apply(lambda x: x + 1)

df
import pandas as pd

df = pd.DataFrame({'A': ["a", "b", "c"], 'B': [4, 5, 6]})


df['A'] = df['A'].str.upper() + "1"
df['B'] += 1

df


참고자료