本文介绍了如何在 pandas 中映射列值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的文件中,一列包含不同的成绩(列名='成绩).

In my files one column containing different grades( columns name='Grades).

例如:91 50K,92 60K,DIESEL,ADBlU等.

eg: 91 50K,92 60K,DIESEL,ADBlU etc..

对于所有这些年级,我需要将其分类为几个年级;

For all these grades I need to categorize them in few grades;

例如: 91 50K= Petrol

在我的python中,我该怎么做?请注意,我可以将整个列传递给函数.但是函数必须用正确的值重写每一行的值;

In my python, how can i do this? Note that i can pass the whole column to the function. but function has to rewrite each row's value with the correct one;

def checkgrades(data):
df['Grades']=???
???

按照下面的答案,我尝试;

As per below answers I try;

 df_dips=map_dips_grades(df_dips)
df_sales = df_sales.merge(df_dips, left_on=['Site Name', 'Date','GradeNo'],
                              right_on=['Site', 'Dip Time', 'Product'], how='left').fillna(0)



def map_dips_grades(data):

    d1 = {'Diesel': ['DIESEL', 'DIESEL 1'],
          'Unleaded': ['91','91 UNLEADED'],
          'PULP':['95','95 ULP'],
          'PULP98':['98','98 20K'],
          'Vortex Diesel':['DIESEL ULT R'],
          'Adblue':['ADBLU','ADO']}

    d = {k: oldk for oldk, oldv in d1.items() for k in oldv}

    data['Product'].map(d)
    return data

但是我明白了

ValueError: You are trying to merge on int64 and object columns. If you wish to proceed you should use pd.concat

推荐答案

您可以在Grades中创建所有可能值的字典,然后在 Series.map :

You can create dictionary of all possible values in Grades and then Series.map:

#test all possible unique values
print (df['Grades'].unique())

d = {'91 50K':'Petrol','92 60K':'Petrol','DIESEL':'Diesel',...}

df['Grades'] = df['Grades'].map(d)

另一种可能减少打字的字典是列表字典:

Another possible dictionary for less typing is dict of lists:

d1 = {'Petrol':['91 50K','92 60K'],
      'Diesel':['DIESEL']}

#swap key values in dict
#http://stackoverflow.com/a/31674731/2901002
d = {k: oldk for oldk, oldv in d1.items() for k in oldv}
print (d)
{'91 50K': 'Petrol', '92 60K': 'Petrol', 'DIESEL': 'Diesel'}

df['Grades'] = df['Grades'].map(d)

这篇关于如何在 pandas 中映射列值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 18:22