Created
          July 24, 2023 11:13 
        
      - 
      
- 
        Save M0nteCarl0/51cb5d1fb36d4694b68c480af578d4b2 to your computer and use it in GitHub Desktop. 
    Complex pandas example
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | import pandas as pd | |
| # Создаем пример DataFrame | |
| data = { | |
| 'Name': ['John', 'Alice', 'Bob', 'Charlie'], | |
| 'Age': [25, 30, 35, 40], | |
| 'Country': ['USA', 'Canada', 'UK', 'Australia'] | |
| } | |
| df = pd.DataFrame(data) | |
| # Функция для авторазметки | |
| def label(row): | |
| if row['Age'] < 30 and row['Country'] == 'USA': | |
| return 'Young American' | |
| elif row['Age'] >= 30 and row['Country'] == 'USA': | |
| return 'Adult American' | |
| elif row['Age'] < 30 and row['Country'] != 'USA': | |
| return 'Young Non-American' | |
| elif row['Age'] >= 30 and row['Country'] != 'USA': | |
| return 'Adult Non-American' | |
| else: | |
| return 'Unknown' | |
| # Применяем функцию авторазметки к DataFrame | |
| df['Label'] = df.apply(label, axis=1) | |
| # Выводим результат | |
| print(df) | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment