数据如下
no,store_id,revenue,profit,state,country
'0','101','779183','281257','WD','India'
'1','101','144829','838451','WD','India'
'2','101','766465','757565','AL','Japan'
'3','102','766465','757565','AL','Japan'
代码在下面
import pandas as pd
data = pd.read_csv("1.csv")
dummies = pd.get_dummies(data)
dummies.head(10)
data.info()是所有列的对象
。
如何自动转换到new object column
到dummies,例如这里的team
是object
需要转换到get_dummies。 如果有人添加了明天的names
列,也需要转换为dummies
对象
int
转换为数字列,将object
转换为非数字列
使用pd.read_csv
读取CSV文件时,将quotechar
参数设置为'
(默认值为“
)
from io import StringIO
text = """no,store_id,revenue,profit,state,country
'0','101','779183','281257','WD','India'
'1','101','144829','838451','WD','India'
'2','101','766465','757565','AL','Japan'
'3','102','766465','757565','AL','Japan'"""
df = pd.read_csv(StringIO(text),quotechar='\'') # or quotechar = "'"
print(df.dtypes)
no int64
store_id int64
revenue int64
profit int64
state object
country object
dtype: object