{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import pandas as pd" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0 a-a-x \n", "1 b-x-b \n", "2 x-c-c \n", "dtype: object\n" ] } ], "source": [ "s = pd.Series([' a-a-x ', ' b-x-b ', ' x-c-c '])\n", "print(s)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0 a-a-z \n", "1 b-z-b \n", "2 z-c-c \n", "dtype: object\n" ] } ], "source": [ "s_new = s.str.replace('x', 'z')\n", "print(s_new)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " col1 col2\n", "0 a-a-x-1 a-a-x-2 \n", "1 b-x-b-1 b-x-b-2 \n", "2 x-c-c-1 x-c-c-2 \n" ] } ], "source": [ "df = pd.DataFrame([[' a-a-x-1 ', ' a-a-x-2 '],\n", " [' b-x-b-1 ', ' b-x-b-2 '],\n", " [' x-c-c-1 ', ' x-c-c-2 ']],\n", " columns=['col1', 'col2'])\n", "print(df)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " col1 col2\n", "0 a-a-z-1 a-a-x-2 \n", "1 b-z-b-1 b-x-b-2 \n", "2 z-c-c-1 x-c-c-2 \n" ] } ], "source": [ "df['col1'] = df['col1'].str.replace('x', 'z')\n", "print(df)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0 a-a-x\n", "1 b-x-b\n", "2 x-c-c\n", "dtype: object\n" ] } ], "source": [ "s_new = s.str.strip()\n", "print(s_new)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0 a-a-\n", "1 b-x-b\n", "2 -c-c\n", "dtype: object\n" ] } ], "source": [ "s_new = s.str.strip(' x')\n", "print(s_new)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " col1 col2\n", "0 a-a-z-1 a-a-x-2 \n", "1 b-z-b-1 b-x-b-2 \n", "2 z-c-c-1 x-c-c-2 \n" ] } ], "source": [ "df['col1'] = df['col1'].str.strip()\n", "print(df)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0 a-a-x \n", "1 b-x-b \n", "2 x-c-c \n", "dtype: object\n" ] } ], "source": [ "s_new = s.str.lstrip()\n", "print(s_new)" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0 a-a-x\n", "1 b-x-b\n", "2 x-c-c\n", "dtype: object\n" ] } ], "source": [ "s_new = s.str.rstrip()\n", "print(s_new)" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0 Hello World\n", "1 hello world\n", "2 HELLO WORLD\n", "dtype: object\n" ] } ], "source": [ "s = pd.Series(['Hello World', 'hello world', 'HELLO WORLD'])\n", "print(s)" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0 hello world\n", "1 hello world\n", "2 hello world\n", "dtype: object\n" ] } ], "source": [ "s_new = s.str.lower()\n", "print(s_new)" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0 HELLO WORLD\n", "1 HELLO WORLD\n", "2 HELLO WORLD\n", "dtype: object\n" ] } ], "source": [ "s_new = s.str.upper()\n", "print(s_new)" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0 Hello world\n", "1 Hello world\n", "2 Hello world\n", "dtype: object\n" ] } ], "source": [ "s_new = s.str.capitalize()\n", "print(s_new)" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0 Hello World\n", "1 Hello World\n", "2 Hello World\n", "dtype: object\n" ] } ], "source": [ "s_new = s.str.title()\n", "print(s_new)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.3" } }, "nbformat": 4, "nbformat_minor": 2 }