对于这个数据集,我检查了实际数据并在列中找到了空格。
所以首先我们需要对数据进行清理,然后我们可以对其进行转换。
为了清洁,我们需要修剪空白。为此,我编写trim_all_the_columns
了删除所有空格的函数
上述数据集的代码
#!/usr/bin/env python
# coding: utf-8
#import required packages
import pandas as pd
import sqlite3 as db
import pandasql as ps
from pandasql import sqldf
#give the path location from where data is loaded, in your case give "https://archive.ics.uci.edu/ml/machine-learning-databases/adult/adult.data"
inputpath=r'C:\Users\Z0040B9K\Desktop\SIGShowcase\adult.data.txt'
#Trim whitespace from ends of each value across all series in dataframe
def trim_all_the_columns(df):
trim_strings = lambda x: x.strip() if type(x) is str else x
return df.applymap(trim_strings)
#creating dataframe
dataSet = pd.read_csv(inputpath,header=None)
#calling trim function over the dataframe to remove all whitespaces
dataSet = trim_all_the_columns(dataSet)
type(dataSet)
dataSet.columns = ['age', 'workclass','fnlwgt','education','education_num','marital_status','occupation','relationship' ,'race','sex','capital_gain','capital_loss','hours_per_week','native_country','salary']
#sql query
q1 = "select distinct sex from dataSet where sex='Male';"
#this will return distinct result of **Male** column and that will be 0
# If you add any other column like **age** or something else you will get result
#q1 = "select distinct age,sex from dataSet where sex='Male';"
pysqldf = lambda q: sqldf(q, globals())
#print result
print(pysqldf(q1))
#this also can be use to print result
print(ps.sqldf(q1, locals()))
查找结果输出:for query
q1 = "select distinct age, sex from dataSet where sex='Male';"
查找查询的结果输出
q1 = "select distinct sex from dataSet where sex='Male';"