在c1_ind和c2_ind中的所有值中查找mode。我不想在每一列中查找mode。
import pandas as pd
import numpy as np
from scipy.stats import mode
list =[{"col1":123,"C1_IND ":"Rev","C2_IND":"Hold"},
{"col1":456,"C1_IND ":"Hold","C2_IND":"Rev"},
{"col1":123,"C1_IND ":"Hold","C2_IND":"Service"},
{"col1":1236,"C1_IND ":"Man","C2_IND":"Man"}]
df = pd.DataFrame.from_dict(list)
print(df)
对于另一个示例,在c1_ind和c3_ind中的所有值中查找模式
import pandas as pd
import numpy as np
from scipy.stats import mode
list =[{"col1":123,"C1_IND ":"Rev","C2_IND":"Hold","C3_IND":"Hold"},
{"col1":456,"C1_IND ":"Hold","C2_IND":"Rev","C3_IND":"Rev"},
{"col1":123,"C1_IND ":"Hold","C2_IND":"Service","C3_IND":"Service"},
{"col1":1236,"C1_IND ":"Man","C2_IND":"Man","C3_IND":"Man"}]
df = pd.DataFrame.from_dict(list)
print(df)
1条答案
按热度按时间bvjxkvbb1#
您可以使用
filter
来选择感兴趣的列(这里使用regex),然后使用stack
,最后使用mode
。iloc[0]
,可选替换为squeeze
。*输出:
'Hold'