在Pandas中创建带有数字的新列以与具有现有数字的列分组 [英] Creating a new column with numbers in Pandas to group with a column with existing numbers
问题描述
美好的一天
我在这里有一个数据框的列:
I have a column from a data frame here:
A
23
10
11
22
我的目标是创建一个新列并关联数字,如下所示:
My objective is to create a new column and associate the numbers like this:
A file_number
23 8
10 6
11 6
22 8
如上所示,数字22、23都与数字8相关联,数字10和11与数字6相关联.如何创建这样的列?预先感谢
As seen above both numbers 22, 23 are associated with the number 8 and numbers 10 and 11 are associated with number 6. How can I create such a column? Thanks in advance
推荐答案
我认为如果需要使用 map
:
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
详细信息:
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})
如果可能需要将正数转换为第一个数字,请使用此解决方案转换为numpy/pandas
:
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
这篇关于在Pandas中创建带有数字的新列以与具有现有数字的列分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!