我正在处理python项目,正在处理csv文件中的数据,并且试图通过将数据从字符串转换为浮点值来将用户数据转换为可用的内容。 这是一列与此相关的数据
F10
White
White
Black
Asian-Pac-Islander
White
Black
...
这是我正在使用的代码;
trainData = pd.read_csv('train.csv')
trainData['F10'].map({' White': 0, ' Black': 1, ' Asian-Pac-Islander': 2, 'white': 0, 'White': 0, 'Black': 1, 'Asian-Pac-Islander': 2, " White": 0, " Black": 1, " Asian-Pac-Islander": 2, "white": 0, "White": 0, "Black": 1, "Asian-Pac-Islander": 2})
不幸的是,尽管如此,当我打印出trainData时,仍然显示字符串Asian-Pac-Islander,White,Black。 I've been using this as a guide,我在做什么错了?
答案 0 :(得分:1)
str.lower()
来实现。str.strip()
删除开头和结尾的空格import pandas as pd
# test dataframe
trainData = pd.DataFrame({'F10': ['White ', 'White', 'Black', ' Asian-Pac-Islander', ' White ', ' Black']})
# strip, lower and map
trainData['F10_numeric'] = trainData['F10'].str.strip().str.lower().map({'white': 0, 'black': 1, 'asian-pac-islander': 2})
# display(trainData)
F10 F10_numeric
0 White 0
1 White 0
2 Black 1
3 Asian-Pac-Islander 2
4 White 0
5 Black 1
F10
列,而只会针对映射进行调整。F10
,然后将值映射到数字。# fix F10
trainData['F10'] = trainData['F10'].str.strip().str.lower()
# map on the fixed F10 column
trainData['F10_numeric'] = trainData['F10'].map({'white': 0, 'black': 1, 'asian-pac-islander': 2})
# display(trainData)
F10 F10_numeric
0 white 0
1 white 0
2 black 1
3 asian-pac-islander 2
4 white 0
5 black 1
答案 1 :(得分:0)
如果要更改数据框中的实际列,则必须使用以下内容:
version: '3'
services:
nginx:
image: nginx:latest
container_name: nginx
ports:
- "80:80"
- "443:443"
volumes:
- ./docker/nginx/core:/etc/nginx/conf.d
- ./:/var/www/
- ./docker/nginx/logs:/var/log/nginx/
links:
- php
php:
build: .
container_name: php-fpm
environment:
XDEBUG_CONFIG: remote_host=host.docker.internal remote_enable=1
volumes:
- ./:/var/www/
- ./docker/php/logs:/var/log/
请注意,我使用了trainData['F10'].replace({' White': 0, ' Black': 1, ' Asian-Pac-Islander': 2, 'white': 0, 'White': 0, 'Black': 1, 'Asian-Pac-Islander': 2, " White": 0, " Black": 1, " Asian-Pac-Islander": 2, "white": 0, "White": 0, "Black": 1, "Asian-Pac-Islander": 2}, inplace=True)
不需要inplace=True
的更好的替代方法是:
inplace=True