
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Write a Python function to split the string based on delimiter and convert to series
The result for splitting the string with ’' delimiter and convert to series as,
0 apple 1 orange 2 mango 3 kiwi
To solve this, we will follow the below approach −
Solution 1
define a function split_str() which accepts two arguments string and delimiter
Create s.split() function inside delimiter value and store it as split_data
split_data = s.split(d)
Apply split_data inside pd.Series() to generate series data.
pd.Series(split_data)
Finally, call the function to return the result.
Example
Let’s check the following code to get a better understanding −
import pandas as pd def split_str(s,d): split_data = s.split(d) print(pd.Series(split_data)) split_str('apple\torange\tmango\tkiwi','\t')
Output
0 apple 1 orange 2 mango 3 kiwi dtype: object
Solution 2
Define a string and assign it to the data variable
data = 'apple\torange\tmango\tkiwi'
Set delimiter = ’
Create lambda function and set two variables x as a string, y as delimiter with expression as x.split(y) and store it as split_data
split_data = lambda x,y: x.split(y)
Call the function with data and delimiter values and save it as a result list
result = split_data(data,delimiter)
Convert the result list to series as,
pd.Series(result)
Example
Let’s check the following code to get a better understanding −
import pandas as pd data = 'apple\torange\tmango\tkiwi' delimiter = '\t' split_data = lambda x,y: x.split(y) result = split_data(data,delimiter) print(pd.Series(result))
Output
0 apple 1 orange 2 mango 3 kiwi dtype: object
- Related Articles
- How to split string by a delimiter string in Python?
- How to split a string with a string delimiter in C#?
- How do we use a delimiter to split string in Python regular expression?
- Java regex program to split a string with line endings as delimiter
- Write a program in Python to verify camel case string from the user, split camel cases, and store them in a new series
- How to split string on whitespace in Python?
- Write a Python code to combine two given series and convert it to a dataframe
- Python program to split and join a string?
- Write a program to truncate a dataframe time series data based on index value
- Write a C program to convert uppercase to lowercase letters without using string convert function
- Write a Python program to separate a series of alphabets and digits and convert them to a dataframe
- How can we split a string by sentence as a delimiter in Java?
- How to split a string in Python
- Write down a python function to convert camel case to snake case?
- Python program to convert a list to a set based on a common element
