Python-pandas-working-with-text-data

提供:Dev Guides
移動先:案内検索

Python Pandas-テキストデータの操作

この章では、基本的なシリーズ/インデックスを使用した文字列操作について説明します。 後続の章では、これらの文字列関数をDataFrameに適用する方法を学習します。

Pandasは、文字列データの操作を容易にする一連の文字列関数を提供します。 最も重要なことは、これらの関数は欠損/NaN値を無視(または除外)することです。

ほとんどの場合、これらのメソッドはすべてPython文字列関数で機能します(https://docs.python.org/3/library/stdtypesl#string-methodsを参照)。 そのため、SeriesオブジェクトをStringオブジェクトに変換してから、操作を実行します。

各操作の実行方法を見てみましょう。

Sr.No Function & Description
1

lower()

シリーズ/インデックスの文字列を小文字に変換します。

2

upper()

Series/Indexの文字列を大文字に変換します。

3

len()

String length()を計算します。

4

strip()

両側のSeries/indexの各文字列から空白(改行を含む)を取り除くのに役立ちます。

5

split(' ')

指定されたパターンで各文字列を分割します。

6

cat(sep=' ')

シリーズ/インデックス要素を指定されたセパレータで連結します。

7

get_dummies()

ワンホットエンコード値でDataFrameを返します。

8

contains(pattern)

部分文字列が要素に含まれる場合は各要素に対してブール値Trueを返し、そうでない場合はFalseを返します。

9

replace(a,b)

a を値 b に置き換えます。

10

repeat(value)

指定された回数で各要素を繰り返します。

11

count(pattern)

各要素のパターンの出現回数を返します。

12

startswith(pattern)

Series/Indexの要素がパターンで始まる場合、trueを返します。

13

endswith(pattern)

Series/Indexの要素がパターンで終わる場合、trueを返します。

14

find(pattern)

パターンが最初に現れる最初の位置を返します。

15

findall(pattern)

パターンのすべての出現のリストを返します。

16

swapcase

ケースの下部/上部を交換します。

17

islower()

シリーズ/インデックスの各文字列のすべての文字が小文字かどうかを確認します。 ブール値を返します

18

isupper()

Series/Indexの各文字列のすべての文字が大文字かどうかを確認します。 ブール値を返します。

19

isnumeric()

シリーズ/インデックスの各文字列のすべての文字が数値であるかどうかを確認します。 ブール値を返します。

ここでシリーズを作成し、上記のすべての機能がどのように機能するかを見てみましょう。

import pandas as pd
import numpy as np

s = pd.Series(['Tom', 'William Rick', 'John', 'Alber@t', np.nan, '1234','SteveSmith'])

print s

その*出力*は次のとおりです-

0            Tom
1   William Rick
2           John
3        Alber@t
4            NaN
5           1234
6    Steve Smith
dtype: object

lower()

import pandas as pd
import numpy as np

s = pd.Series(['Tom', 'William Rick', 'John', 'Alber@t', np.nan, '1234','SteveSmith'])

print s.str.lower()

その*出力*は次のとおりです-

0            tom
1   william rick
2           john
3        alber@t
4            NaN
5           1234
6    steve smith
dtype: object

アッパー()

import pandas as pd
import numpy as np

s = pd.Series(['Tom', 'William Rick', 'John', 'Alber@t', np.nan, '1234','SteveSmith'])

print s.str.upper()

その*出力*は次のとおりです-

0            TOM
1   WILLIAM RICK
2           JOHN
3        ALBER@T
4            NaN
5           1234
6    STEVE SMITH
dtype: object

len()

import pandas as pd
import numpy as np

s = pd.Series(['Tom', 'William Rick', 'John', 'Alber@t', np.nan, '1234','SteveSmith'])
print s.str.len()

その*出力*は次のとおりです-

0    3.0
1   12.0
2    4.0
3    7.0
4    NaN
5    4.0
6   10.0
dtype: float64

ストリップ()

import pandas as pd
import numpy as np
s = pd.Series(['Tom ', ' William Rick', 'John', 'Alber@t'])
print s
print ("After Stripping:")
print s.str.strip()

その*出力*は次のとおりです-

0            Tom
1   William Rick
2           John
3        Alber@t
dtype: object

After Stripping:
0            Tom
1   William Rick
2           John
3        Alber@t
dtype: object

split(パターン)

import pandas as pd
import numpy as np
s = pd.Series(['Tom ', ' William Rick', 'John', 'Alber@t'])
print s
print ("Split Pattern:")
print s.str.split(' ')

その*出力*は次のとおりです-

0            Tom
1   William Rick
2           John
3        Alber@t
dtype: object

Split Pattern:
0   [Tom, , , , , , , , , , ]
1   [, , , , , William, Rick]
2   [John]
3   [Alber@t]
dtype: object

cat(sep = pattern)

import pandas as pd
import numpy as np

s = pd.Series(['Tom ', ' William Rick', 'John', 'Alber@t'])

print s.str.cat(sep='_')

その*出力*は次のとおりです-

Tom _ William Rick_John_Alber@t

get_dummies()

import pandas as pd
import numpy as np

s = pd.Series(['Tom ', ' William Rick', 'John', 'Alber@t'])

print s.str.get_dummies()

その*出力*は次のとおりです-

   William Rick   Alber@t   John   Tom
0             0         0      0     1
1             1         0      0     0
2             0         0      1     0
3             0         1      0     0

含む()

import pandas as pd

s = pd.Series(['Tom ', ' William Rick', 'John', 'Alber@t'])

print s.str.contains(' ')

その*出力*は次のとおりです-

0   True
1   True
2   False
3   False
dtype: bool

replace(a、b)

import pandas as pd
s = pd.Series(['Tom ', ' William Rick', 'John', 'Alber@t'])
print s
print ("After replacing @ with $:")
print s.str.replace('@','$')

その*出力*は次のとおりです-

0   Tom
1   William Rick
2   John
3   Alber@t
dtype: object

After replacing @ with $:
0   Tom
1   William Rick
2   John
3   Alber$t
dtype: object

繰り返し(値)

import pandas as pd

s = pd.Series(['Tom ', ' William Rick', 'John', 'Alber@t'])

print s.str.repeat(2)

その*出力*は次のとおりです-

0   Tom            Tom
1   William Rick   William Rick
2                  JohnJohn
3                  Alber@tAlber@t
dtype: object

カウント(パターン)

import pandas as pd

s = pd.Series(['Tom ', ' William Rick', 'John', 'Alber@t'])

print ("The number of 'm's in each string:")
print s.str.count('m')

その*出力*は次のとおりです-

The number of 'm's in each string:
0    1
1    1
2    0
3    0

startswith(パターン)

import pandas as pd

s = pd.Series(['Tom ', ' William Rick', 'John', 'Alber@t'])

print ("Strings that start with 'T':")
print s.str. startswith ('T')

その*出力*は次のとおりです-

0  True
1  False
2  False
3  False
dtype: bool

endswith(パターン)

import pandas as pd
s = pd.Series(['Tom ', ' William Rick', 'John', 'Alber@t'])
print ("Strings that end with 't':")
print s.str.endswith('t')

その*出力*は次のとおりです-

Strings that end with 't':
0  False
1  False
2  False
3  True
dtype: bool

find(パターン)

import pandas as pd

s = pd.Series(['Tom ', ' William Rick', 'John', 'Alber@t'])

print s.str.find('e')

その*出力*は次のとおりです-

0  -1
1  -1
2  -1
3   3
dtype: int64

「-1」は、そのようなパターンが要素にないことを示します。

findall(パターン)

import pandas as pd

s = pd.Series(['Tom ', ' William Rick', 'John', 'Alber@t'])

print s.str.findall('e')

その*出力*は次のとおりです-

0 []
1 []
2 []
3 [e]
dtype: object

Null list([])は、要素にそのようなパターンが存在しないことを示します。

swapcase()

import pandas as pd

s = pd.Series(['Tom', 'William Rick', 'John', 'Alber@t'])
print s.str.swapcase()

その*出力*は次のとおりです-

0  tOM
1  wILLIAM rICK
2  jOHN
3  aLBER@T
dtype: object

islower()

import pandas as pd

s = pd.Series(['Tom', 'William Rick', 'John', 'Alber@t'])
print s.str.islower()

その*出力*は次のとおりです-

0  False
1  False
2  False
3  False
dtype: bool

isupper()

import pandas as pd

s = pd.Series(['Tom', 'William Rick', 'John', 'Alber@t'])

print s.str.isupper()

その*出力*は次のとおりです-

0  False
1  False
2  False
3  False
dtype: bool

isnumeric()

import pandas as pd

s = pd.Series(['Tom', 'William Rick', 'John', 'Alber@t'])

print s.str.isnumeric()

その*出力*は次のとおりです-

0  False
1  False
2  False
3  False
dtype: bool