使用apply函数处理DataFrame时,列的数据类型意外更改。我该怎么做才能防止这种情况发生?
例如:
In [1]: import pandas as pd
In [2]: from pandas import DataFrame
In [3]: tmp = DataFrame({'item':[1,2,3]})
In [4]: tmp['score'] = 0.0
In [5]: tmp.dtypes
Out[5]:
item int64
score float64
dtype: object
In [6]: tmp
Out[6]:
item score
0 1 0.0
1 2 0.0
2 3 0.0
In [7]: def Test(x):
...: return x
...:
In [8]: tmp = tmp.apply(Test,axis=1)
In [9]: tmp.dtypes
Out[9]:
item float64
score float64
dtype: object
tmp['item']
的数据类型已更改为float。如何维护它的原始数据类型?
答案 0 :(得分:0)
这种情况正在发生,因为var data=[{color:"blue",party:"Democratic",text:"California",value:55},{color:"blue",party:"Democratic",text:"Oregon",value:7},{color:"red",party:"Republican",text:"Texas",value:38},{color:"red",party:"Republican",text:"Georgia",value:16},{color:"grey",party:"Democratic",text:"Arizona",value:11}];
var result = data.reduce(function(hash) {
return function(prev, curr) {
if (hash[curr.color]) {
hash[curr.color].children.push({
text: curr.text,
value: curr.value,
style: {
backgroundColor: curr.color
}
});
} else {
hash[curr.color] = {};
hash[curr.color].children = hash[curr.color].children || [];
prev.push({
text: curr.party,
style: {
backgroundColor: curr.color
},
children: hash[curr.color].children
});
hash[curr.color].children.push({
text: curr.text,
value: curr.value,
style: {
backgroundColor: curr.color
}
});
}
return prev;
};
}(Object.create(null)), []);
console.log(result);
基本上遍历行(当轴= 1时)并将函数应用于表示每行的Series。由于.as-console-wrapper{top: 0;max-height: 100%!important;}
必须包含相同的数据类型,因此由一行混合.apply
和Series
类型组成的系列会正确地将int
提升为float
:
ints
请注意我们选择列时会发生什么:
float