python pandas用数字替换数据框中的字符串 [英] python pandas replacing strings in dataframe with numbers
问题描述
有什么方法可以使用映射函数或更好的方法来替换整个数据帧中的值?
Is there any way to use the mapping function or something better to replace values in an entire dataframe?
我只知道如何对系列进行映射.
I only know how to perform the mapping on series.
我想用数字替换test"和set"列中的字符串例如 set = 1, test =2
I would like to replace the strings in the 'tesst' and 'set' column with a number for example set = 1, test =2
这是我的数据集示例:(原始数据集非常大)
Here is a example of my dataset: (Original dataset is very large)
ds_r
respondent brand engine country aware aware_2 aware_3 age tesst set
0 a volvo p swe 1 0 1 23 set set
1 b volvo None swe 0 0 1 45 set set
2 c bmw p us 0 0 1 56 test test
3 d bmw p us 0 1 1 43 test test
4 e bmw d germany 1 0 1 34 set set
5 f audi d germany 1 0 1 59 set set
6 g volvo d swe 1 0 0 65 test set
7 h audi d swe 1 0 0 78 test set
8 i volvo d us 1 1 1 32 set set
最终结果应该是
ds_r
respondent brand engine country aware aware_2 aware_3 age tesst set
0 a volvo p swe 1 0 1 23 1 1
1 b volvo None swe 0 0 1 45 1 1
2 c bmw p us 0 0 1 56 2 2
3 d bmw p us 0 1 1 43 2 2
4 e bmw d germany 1 0 1 34 1 1
5 f audi d germany 1 0 1 59 1 1
6 g volvo d swe 1 0 0 65 2 1
7 h audi d swe 1 0 0 78 2 1
8 i volvo d us 1 1 1 32 1 1
推荐答案
In [9]: mapping = {'set': 1, 'test': 2}
In [10]: df.replace({'set': mapping, 'tesst': mapping})
Out[10]:
Unnamed: 0 respondent brand engine country aware aware_2 aware_3 age
0 0 a volvo p swe 1 0 1 23
1 1 b volvo None swe 0 0 1 45
2 2 c bmw p us 0 0 1 56
3 3 d bmw p us 0 1 1 43
4 4 e bmw d germany 1 0 1 34
5 5 f audi d germany 1 0 1 59
6 6 g volvo d swe 1 0 0 65
7 7 h audi d swe 1 0 0 78
8 8 i volvo d us 1 1 1 32
tesst set
0 2 1
1 1 2
2 2 1
3 1 2
4 2 1
5 1 2
6 2 1
7 1 2
8 2 1
正如@Jeff 在评论中指出的,在 Pandas 版本中 <0.11.1,手动将 .convert_objects()
添加到末尾以正确转换测试并设置为 int64
列,以防在后续操作中很重要.
As @Jeff pointed out in the comments, in pandas versions < 0.11.1, manually tack .convert_objects()
onto the end to properly convert tesst and set to int64
columns, in case that matters in subsequent operations.
这篇关于python pandas用数字替换数据框中的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!