📜  Python Pandas – get_dummies() 方法

📅  最后修改于: 2022-05-13 01:54:54.950000             🧑  作者: Mango

Python Pandas – get_dummies() 方法

pandas.get_dummies()用于数据操作。它将分类数据转换为虚拟变量或指示变量。

示例 1:

Python3
import pandas as pd
 
con = pd.Series(list('abcba'))
print(pd.get_dummies(con))


Python
import pandas as pd
import numpy as np
 
 
# list
li = ['s', 'a', 't', np.nan]
print(pd.get_dummies(li))


Python
import pandas as pd
import numpy as np
 
 
# list
li = ['s', 'a', 't', np.nan]
print(pd.get_dummies(li, dummy_na=True))


Python3
import pandas as pd
import numpy as np
 
 
# dictionary
diff = pd.DataFrame({'R': ['a', 'c', 'd'],
                     'T': ['d', 'a', 'c'],
                     'S_': [1, 2, 3]})
 
print(pd.get_dummies(diff, prefix=['column1', 'column2']))



输出:

输出

示例 2:

Python

import pandas as pd
import numpy as np
 
 
# list
li = ['s', 'a', 't', np.nan]
print(pd.get_dummies(li))

输出:

Nan 列不存在,因为 dummy_na 默认为 False

示例 3:(获取 NaN 列)

Python

import pandas as pd
import numpy as np
 
 
# list
li = ['s', 'a', 't', np.nan]
print(pd.get_dummies(li, dummy_na=True))

输出:

示例 4:

Python3

import pandas as pd
import numpy as np
 
 
# dictionary
diff = pd.DataFrame({'R': ['a', 'c', 'd'],
                     'T': ['d', 'a', 'c'],
                     'S_': [1, 2, 3]})
 
print(pd.get_dummies(diff, prefix=['column1', 'column2']))

输出: