python的新手,我对此深感困惑。我的CSV文件包含以下内容:
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.function.Consumer;
import java.lang.Integer;
public class ForEachExample {
public static void main(String[] args) {
//creating sample Collection
List<Integer> myList = new ArrayList<Integer>();
for(int i=0; i<10; i++) myList.add(i);
//traversing using Iterator
Iterator<Integer> it = myList.iterator();
while(it.hasNext()){
Integer i = it.next();
System.out.println("Iterator Value::"+i);
}
//traversing through forEach method of Iterable with anonymous class
myList.forEach(new Consumer<Integer>() {
public void accept(Integer t) {
System.out.println("forEach anonymous class Value::"+t);
}
});
//traversing with Consumer interface implementation
MyConsumer action = new MyConsumer();
myList.forEach(action);
}
}
//Consumer implementation that can be reused
**class MyConsumer implements Consumer<Integer>{
public void accept(Integer t) {
System.out.println("Consumer impl Value::"+t);
}
}**
现在,我想将Gender值转换为二进制值,以便文件看起来像这样:
Sr,Gender
1,Male
2,Male
3,Female
因此,我将CSV文件导入为Sr,Gender
1,1
2,1
3,0
并运行了以下代码:
data
但是我收到了错误data["Gender_new"]=1
data["Gender_new"][data["Gender"]=="Male"]=0
data["Gender_new"]=1=data["Gender_new"].astype(float)
我在做什么错了,我该怎么做?
谢谢
答案 0 :(得分:1)
尝试一下:
import pandas as pd
file = open("your.csv", "r")
data = pd.read_csv(file, sep = ",")
gender = {'male': 1,'female': 0}
data.Gender = [gender[item] for item in data.Gender]
print(data)
或
data.Gender[data.Gender == 'male'] = 1
data.Gender[data.Gender == 'female'] = 0
print(data)
答案 1 :(得分:0)
您可以在加载文件时进行转换:
d = pandas.read_csv('yourfile.csv', converters={'Gender': lambda x: int(x == 'Male')})
converters
参数采用一个字典,其键是列名(或索引),而值是为每个项目调用的函数。该函数必须返回转换后的值。
另一种方法是在拥有数据框后将其转换,如@DJK在其注释中指出的那样:
data['Gender'] = (data['Gender'] == 'Male').astype(int)