如何获得星号(*)而不是数字0的结果?

时间:2016-07-13 12:00:18

标签: python pandas

如何获得星号(*)而不是数字0的结果?

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'])

我希望在emp数据帧中显示星号(*)而不是列sal的数字0。

我想使用pandas获得以下结果。

result :

5***
285*
245*
2975
125*
16**
15**
95*
125*
3***
8**
3***
11**
13**

代码:

import sys

import pandas as pd
import dateutil



# Load data from csv file
emp = pd.DataFrame.from_csv("D:\R data\emp.csv")
# Convert date from string to date times
emp['hiredate'] = emp['hiredate'].apply(dateutil.parser.parse, dayfirst=True)


print( emp['sal'].replace(3,'f',inplace=True))  <----- I want change this line.

1 个答案:

答案 0 :(得分:2)

供将来参考(我很乐意帮助你):

emp['sal'] = emp['sal'].astype(str)
emp['sal'] = emp['sal'].str.replace('0', '*')

解释:首先我们将列转换为字符串(需要进行替换)。然后我们使用一个漂亮的pandas操作“.str”,它允许你直接在数据系列上使用很多python字符串函数。 (还有一个日期时间变体,您可以在其中执行“.dt.weekday”)

之类的操作