Python | Pandas Series.str.wrap ()

| | | | | | | | | | | | | | | | | | |

👻 Check our latest review to choose the best laptop for Machine Learning engineers and Deep learning tasks!

Pandas str.wrap() is an important technique when working with long text data (paragraphs or messages). This is used to spread long text data onto new lines, or to handle whitespace when it exceeds the passed width. Since this is a string method, you must prefix .str before calling this method.

Syntax: Series .str.wrap (width, ** kwargs)

Parameters:
width: Integer value, defines maximum line width

** kwargs [Optional parameters]
expand_tabs: Boolean value, expands tab characters to spaces if True
replace_whitespace: Boolean value, if true, each white space character is replaced by single white space.
drop_whitespace: Boolean value, If true, removes whitespace if any at the beginning of new lines
break_long_words: Boolean value, if True, breaks word that are longer than the passed width.
break_on_hyphens: Boolean value, if true, breaks string on hyphens where string length is less than width.

Return type: Series with splitted lines / added characters (’’)

To load dataset used in code press here.

In the following examples, the data frame used contains data for some NBA players. An image of the data frame before any operations is attached below.

Example:
In this example, the Team wrapped 5 characters wide. Therefore, / n will appear after every 5 characters. A random item from the new team column and the old team column is printed to see the work. Before any operations are applied, null elements are removed using .dropna() .

# import pandas module

import pandas as pd


# read CSV file from URL

data = pd.read_csv ( " https://media.python.engineering/wp-content/uploads/nba.csv " )


# remove null columns to avoid errors

data.dropna (inplace = True )


# display

data [ "New Team" ] = data [ "Team" ]. str . wrap ( 5 )


# display the data frame
data


# print the same index separately

print (data [ " Team " ] [ 120 ])

print ( "---- -------- " )

print (data [ "New Team" ] [ 120 ])

Output:
As shown in the output images, the new column has & # 39; / n & # 39; after every 5 characters. After printing the same index of the old and new command columns, you can see that without adding a newline character in the print statement, python automatically reads & # 39; / n & # 39; in a line and places it on a new line.

Data frame with New Team column

Exit :

 Los Angeles Lakers ------------ Los A ngele s Lak ers 

👻 Read also: what is the best laptop for engineering students?

We hope this article has helped you to resolve the problem. Apart from Python | Pandas Series.str.wrap (), check other code Python module-related topics.

Want to excel in Python? See our review of the best Python online courses 2023. If you are interested in Data Science, check also how to learn programming in R.

By the way, this material is also available in other languages:



Olivia Robinson

San Francisco | 2023-02-07

I was preparing for my coding interview, thanks for clarifying this - Python | Pandas Series.str.wrap () in Python is not the simplest one. Will use it in my bachelor thesis

Davies Galleotti

Tallinn | 2023-02-07

Thanks for explaining! I was stuck with Python | Pandas Series.str.wrap () for some hours, finally got it done 🤗. Will get back tomorrow with feedback

Schneider Zelotti

California | 2023-02-07

Maybe there are another answers? What Python | Pandas Series.str.wrap () exactly means?. Will get back tomorrow with feedback

Shop

Gifts for programmers

Learn programming in R: courses

$FREE
Gifts for programmers

Best Python online courses for 2022

$FREE
Gifts for programmers

Best laptop for Fortnite

$399+
Gifts for programmers

Best laptop for Excel

$
Gifts for programmers

Best laptop for Solidworks

$399+
Gifts for programmers

Best laptop for Roblox

$399+
Gifts for programmers

Best computer for crypto mining

$499+
Gifts for programmers

Best laptop for Sims 4

$

Latest questions

PythonStackOverflow

Common xlabel/ylabel for matplotlib subplots

1947 answers

PythonStackOverflow

Check if one list is a subset of another in Python

1173 answers

PythonStackOverflow

How to specify multiple return types using type-hints

1002 answers

PythonStackOverflow

Printing words vertically in Python

909 answers

PythonStackOverflow

Python Extract words from a given string

798 answers

PythonStackOverflow

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

606 answers

PythonStackOverflow

Python os.path.join () method

384 answers

PythonStackOverflow

Flake8: Ignore specific warning for entire file

360 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

How to specify multiple return types using type-hints

Printing words vertically in Python

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically