Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric Python packages. Pandas is one of those packages and makes importing and analyzing data much easier.
Pandas provide a method to swap case of each string in a series. This means lower case characters will be converted into uppercase and Uppercase character into lower case in every string. .str has to be prefixed every time before calling this method to differentiate it from Python’s default function otherwise, it will give an error.
Return Type: Series with Swapped case of each character
To download the CSV used in code, click here.
In the following examples, the data frame used contains data of some NBA players. As it can be seen, the text in data frame is mostly in Camel case. In the following examples, str.swapcase() method will be used to interchange case of the text. The image of data frame before any operations is shown below:
In this example, Null rows are removed using dropna() method (All though str.swapcase() doesn’t throw an error for null values, but it’s a good practice to remove them to avoid errors).
After that, the case of Text in Team column have been swapped using .swapcase() method and the results are overwritten in the Team column itself. After that, the Data frame is displayed to view the changes made in the text case of Team column.
As shown in the output image, the text case in Team column has been interchanged.
In this example, a copy of Name column is made. After that str.swapcase() is applied twice on it and it is checked with original Series that whether it’s same or not.
As shown in the output image, the whole data frame was returned when the filter was passed in .where() method. This means after that after doing str.swapcase() twice, the string becomes what it was before operation.
- Python | pandas.to_markdown() in Pandas
- Python | Pandas Panel.sum()
- Python | Pandas DatetimeIndex.day
- Python | Pandas Series.div()
- Python | Pandas Series.pow()
- Python | Pandas Panel.abs()
- Python | Pandas dataframe.std()
- Python | Pandas Series.pop()
- Python | Pandas Series.add()
- Python | Pandas Series.ptp()
- Python | Pandas Series.sub()
- Python | Pandas Series.mul()
- Pandas.cut() method in Python
- Python | Pandas DatetimeIndex.second
- Python | Pandas dataframe.min()
- Python | Pandas dataframe.sub()
- Python | Pandas dataframe.sum()
- Python | Pandas Series.sum()
- Python | Pandas dataframe.take()
- Python | Pandas Series.str.len()
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.