Muchas veces necesitamos extraer caracteres específicos presentes dentro de una string en el marco de datos de Pandas. Para resolver este problema, tenemos el concepto de Izquierda, Derecha y Medio en pandas.
Ejemplo 1: extraer caracteres de la izquierda
Python3
# importing pandas library import pandas as pd # creating and initializing a list Cars = ['1000-BMW','2000-Audi','3000-Volkswagen', '4000-Datsun','5000-Toyota','6000-Maruti Suzuki'] # creating a pandas dataframe df = pd.DataFrame(Cars, columns= ['Model_name']) # Extracting characters from right side # using slicing and storing result in # 'Left' Left = df['Model_name'].str[:4] print(Left)
Producción :
0 1000 1 2000 2 3000 3 4000 4 5000 5 6000 Name: Model_name, dtype: object
Ejemplo 2: extraer caracteres de la derecha
Python3
# importing pandas library import pandas as pd # creating and initializing a list Cars = ['ID-11111-BMW','ID-22222-Volkswagen', 'ID-33333-Toyota','ID-44444-Hyundai ', 'ID-55555-Datsun','ID-66666-Mercedes'] # creating a pandas dataframe df = pd.DataFrame(Cars, columns= ['Model_name']) # Extracting characters from left side using # slicing and storing result in 'Right' Right = df['Model_name'].str[4:8] print (Right)
Producción :
0 11111 1 22222 2 33333 3 44444 4 55555 5 66666 Name: Model_name, dtype: object
Ejemplo 3: extraer caracteres del medio
Python3
# importing pandas library import pandas as pd # creating and initializing a list Cars = ['ID-11111-BMW','ID-22222-Volkswagen', 'ID-33333-Toyota','ID-44444-Hyundai ', 'ID-55555-Datsun','ID-66666-Mercedes'] # creating a pandas dataframe df = pd.DataFrame(Cars, columns= ['Model_name']) # Extracting characters from Middle using # slicing and storing result in 'Mid' Mid = df['Model_name'].str[4:8] print (Mid)
Producción :
0 1111 1 2222 2 3333 3 4444 4 5555 5 6666 Name: Model_name, dtype: object
Ejemplo 4: Antes de un símbolo usando la función str.split()
Python3
# importing pandas library import pandas as pd # creating and initializing a list Cars = ['1000-BMW','2000-Audi', '3000-Volkswagen','4000-Datsun', '5000-Toyota','6000-Maruti Suzuki'] # creating a pandas dataframe df = pd.DataFrame(Cars, columns= ['Model_name']) # Extracting characters before symbol "-" # using srt.strip() and str[0] # and storing result to 'Before_symbol' Before_symbol = df['Model_name'].str.split('-').str[0] print (Before_symbol)
Producción :
0 1000 1 2000 2 3000 3 4000 4 5000 5 6000 Name: Model_name, dtype: object
Ejemplo 5: Entre símbolos idénticos usando la función str.split()
Python3
# importing pandas library import pandas as pd # creating and initializing a list Cars = ['M3-1906-BMW','M5-2096-Audi', 'M11-3096-Volkswagen','M9-4096-Datsun', 'M8-5096-Toyota','M23-6096-Maruti Suzuki'] # creating a pandas dataframe df = pd.DataFrame(Cars, columns= ['Model_name']) # Extracting characters between symbol "-" # using srt.strip() and str[1] # and storing result to 'Before_symbol' BetweenTwoSymbols = df['Model_name'].str.split('-').str[1] print (BetweenTwoSymbols)
Producción :
0 1906 1 2096 2 3096 3 4096 4 5096 5 6096 Name: Model_name, dtype: object
Publicación traducida automáticamente
Artículo escrito por vanshgaur14866 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA