본문 바로가기
python/pandas

pandas - DataFrame에 들어있는 데이터를 함수에 적용하기( apply( ) )

by leo104 2022. 11. 25.
728x90

DataFrame에 들어있는 데이터의 글자수 구하기

- apply 함수 안에, 내가 적용하고 싶은 함수의 이름만 써준다.

# 직원 이름이 몇글자인지, 이름 글자수를 구해서
# 새로운 컬럼 length 라는 컬럼에 저장하세요.

df

Employee ID	Employee Name	Salary [$/h]	Years of Experience
0	111	Chanel	35	3
1	222	Steve	29	4
2	333	Mitch	38	9
3	444	Bird	20	1

# apply 함수 안에, 내가 적용하고 싶은 함수의 이름만 써준다.

df['length'] = df['Employee Name']. apply(len)

df

Employee ID	Employee Name	Salary [$/h]	Years of Experience	length
0	111	Chanel	35	3	6
1	222	Steve	29	4	5
2	333	Mitch	38	9	5
3	444	Bird	20	1	4

# 'Employee Name'의 이름의 문자 개수를 구해서
# 새로운 칼럼 length2에 저장하세요.
# 문자의 개수는 .str.len( )을 이용한다.

df['length2'] = df['Employee Name'].str.len()

df

Employee ID	Employee Name	Salary [$/h]	Years of Experience	length	length2
0	111	Chanel	35	3	6	6
1	222	Steve	29	4	5	5
2	333	Mitch	38	9	5	5
3	444	Bird	20	1	4	4
728x90