Python assign same value in another column if the values in this column match -


i imported table excel. below code:

import pandas pd  table = pd.excelfile('c:/users/sophia wu/master data_pms_1110.xlsx') table.sheet_names df = table.parse('sheet1') 

the table looks this:

| fruit | color |

| apple | red |

| pear | yellow |

| grape | purple |

| apple | |

| grape | |

i need assign same color if same type of fruit. since have more 10,000 rows in table, how automatically assign values using python?

thanks lot!

try this, (untested since @ work):

colors_to_match = {'apple':'red','pear':'yellow','grape':'purple'} df['color'] = df['fruit'].map(colors_to_match) 

basically making table, , mapping backed on values in fruit , setting color using map function panda


Comments

Popular posts from this blog

sql server - Cannot query correctly (MSSQL - PHP - JSON) -

php - trouble displaying mysqli database results in correct order -

C++ Linked List -