yu yeon su
yu yeon su

Reputation: 61

How should I receive first string from columns in dataframe using pandas?

emp.csv

import pandas as pd
import io

temp=u"""index   empno   ename   job mgr hiredate    sal comm    deptno
0,  7839,   KING,   PRESIDENT,  0,  1981-11-17,     5000,   0,  10
1,  7698,   BLAKE,  MANAGER,    7839,   1981-05-01, 2850,   0,  30
2,  7782,   CLARK,  MANAGER,    7839,   1981-05-09, 2450,   0,  10
3,  7566,   JONES,  MANAGER,    7839,   1981-04-01, 2975,   0,  20
4,  7654,   MARTIN, SALESMAN,   7698,   1981-09-10, 1250,   1400,   30
5,  7499,   ALLEN,  SALESMAN,   7698,   1981-02-11, 1600,    300,    30
6,  7844,   TURNER, SALESMAN,   7698,   1981-08-21, 1500,   0,  30
7,  7900,   JAMES,  CLERK,      7698,   1981-12-11, 950,    0,  30
8,  7521,   WARD,   SALESMAN,   7698,   1981-02-23, 1250,   500,    30
9,  7902,   FORD,   ANALYST,    7566,   1981-12-11, 3000,   0,  20
10, 7369,   SMITH,  CLERK,      7902,   1980-12-09, 800,    0,  20
11, 7788,   SCOTT,  ANALYST,    7566,    1982-12-22, 3000,   0,  20
12, 7876,   ADAMS,  CLERK,      7788,   1983-01-15, 1100,   0,  20
13, 7934,   MILLER, CLERK,      7782,   1982-01-11, 1300,   0,  10"""
#after testing replace io.StringIO(temp) to filename
emp = pd.read_csv(io.StringIO(temp), 
                 skipinitialspace=True,
                 skiprows=1, 
                 parse_dates=[5], 
                 names=['index','empno','ename', 'job','mgr','hiredate','sal','comm','deptno'])

I want to get first string of column ename such as below result using pandas.

K
B
C
J
M
A
T
J
W
F
S
S
A
M

How should I get to above result using pandas ?

code :

import pandas as pd
import io

temp=u"""index   empno   ename   job mgr hiredate    sal comm    deptno
0,  7839,   KING,   PRESIDENT,  0,  1981-11-17,     5000,   0,  10
1,  7698,   BLAKE,  MANAGER,    7839,   1981-05-01, 2850,   0,  30
2,  7782,   CLARK,  MANAGER,    7839,   1981-05-09, 2450,   0,  10
3,  7566,   JONES,  MANAGER,    7839,   1981-04-01, 2975,   0,  20
4,  7654,   MARTIN, SALESMAN,   7698,   1981-09-10, 1250,   1400,   30
5,  7499,   ALLEN,  SALESMAN,   7698,   1981-02-11, 1600,    300,    30
6,  7844,   TURNER, SALESMAN,   7698,   1981-08-21, 1500,   0,  30
7,  7900,   JAMES,  CLERK,      7698,   1981-12-11, 950,    0,  30
8,  7521,   WARD,   SALESMAN,   7698,   1981-02-23, 1250,   500,    30
9,  7902,   FORD,   ANALYST,    7566,   1981-12-11, 3000,   0,  20
10, 7369,   SMITH,  CLERK,      7902,   1980-12-09, 800,    0,  20
11, 7788,   SCOTT,  ANALYST,    7566,    1982-12-22, 3000,   0,  20
12, 7876,   ADAMS,  CLERK,      7788,   1983-01-15, 1100,   0,  20
13, 7934,   MILLER, CLERK,      7782,   1982-01-11, 1300,   0,  10"""
#after testing replace io.StringIO(temp) to filename
emp = pd.read_csv(io.StringIO(temp), 
                 skipinitialspace=True,
                 skiprows=1, 
                 parse_dates=[5], 
                 names=['index','empno','ename', 'job','mgr','hiredate','sal','comm','deptno'])

    emprusult =   ?   <--- i want to this code.

    print(emprusult)

How can I get to first string of column ename using pandas ?

Upvotes: 2

Views: 68

Answers (1)

dot.Py
dot.Py

Reputation: 5147

You can try this code:

emp['firstEname'] = emp['ename'].apply(lambda x: x[0])

It will create another column called firstEname, and fill it with the first character of each ename occurrence.

Output for emp['firstEname']:

0     K
1     B
2     C
3     J
4     M
5     A
6     T
7     J
8     W
9     F
10    S
11    S
12    A
13    M
Name: firstEname, dtype: object

Upvotes: 2

Related Questions