I think need if need create new values by first value of number with map
by dictionary:
print (df['A'].apply(type))
0 <class 'int'>
1 <class 'int'>
2 <class 'int'>
3 <class 'int'>
Name: A, dtype: object
df['new'] = (df['A'] // 10).map({1:6, 2:8})
print (df)
A new
0 23 8
1 10 6
2 11 6
3 22 8
Detail:
print ((df['A'] // 10))
0 2
1 1
2 1
3 2
Name: A, dtype: int64
Another solution works with strings:
df['new'] = df['A'].astype(str).str[0].map({'1':6, '2':8})
print (df['A'].apply(type))
0 <class 'str'>
1 <class 'str'>
2 <class 'str'>
3 <class 'str'>
Name: A, dtype: object
df['new'] = df['A'].str[0].map({'1':6, '2':8})
If need convert positive number to first numeric is possible use this solution converted to numpy/pandas
:
df['new'] = df['A'] // 10 ** np.log10(df['A'].values).astype(int)
print (df)
A new
0 2 2
1 10000 1
2 110 1
3 220000 2
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…