2016-12-13 20 views
5

のすべてのセルの値から接頭辞/接尾辞を追加&削除、私は通常、接尾辞'@'を追加するために、例えば...パンダのデータフレーム:データフレームに接頭辞/接尾辞を追加するには、全データフレーム

次の操作を行い、

df = df.astype(str) + '@' 

これは基本的にすべてのセル値に'@'を追加しました。

この接尾辞を削除する方法を知りたいと思います。 DataFrame全体から特定のプレフィックス/サフィックス文字を直接削除するpandas.DataFrameクラスで使用できるメソッドはありますか?

私は次のようにrstrip('@')を使用している間(シリーズとして)行を反復処理しようとしました:

for index in range(df.shape[0]): 
    row = df.iloc[index] 
    row = row.str.rstrip('@') 

、このシリーズのうち、データフレームを作るために、

new_df = pd.DataFrame(columns=list(df)) 
new_df = new_df.append(row) 

をしかし、これは機能しません。 空のデータフレームを与えます。

本当に基本的なものがありますか?

あなたが applyを使用することができます

答えて

3

あなたは各要素に、あなたの文字列の方法を適用するためにapplymap使用することができます

df = df.applymap(lambda x: str(x).rstrip('@')) 
+0

完璧! Thanks Alex :) – murphy1310

5

とpd.Seriesのstr.strip方法:

In [13]: df 
Out[13]: 
     a  b  c 
0 dog quick the 
1 lazy lazy fox 
2 brown quick dog 
3 quick  the over 
4 brown over lazy 
5 fox brown quick 
6 quick  fox the 
7 dog jumped the 
8 lazy brown the 
9 dog lazy the 

In [14]: df = df + "@" 

In [15]: df 
Out[15]: 
     a  b  c 
0 [email protected] [email protected] [email protected] 
1 [email protected] [email protected] [email protected] 
2 [email protected] [email protected] [email protected] 
3 [email protected]  [email protected] [email protected] 
4 [email protected] [email protected] [email protected] 
5 [email protected] [email protected] [email protected] 
6 [email protected]  [email protected] [email protected] 
7 [email protected] [email protected] [email protected] 
8 [email protected] [email protected] [email protected] 
9 [email protected] [email protected] [email protected] 

In [16]: df = df.apply(lambda S:S.str.strip('@')) 

In [17]: df 
Out[17]: 
     a  b  c 
0 dog quick the 
1 lazy lazy fox 
2 brown quick dog 
3 quick  the over 
4 brown over lazy 
5 fox brown quick 
6 quick  fox the 
7 dog jumped the 
8 lazy brown the 
9 dog lazy the 

注意、あなたのアプローチは機能しないためには、次の割り当てを行うときに、あなたのforループ:

row = row.str.rstrip('@') 

DataFrameに変更を加えずに、の結果をrowという名前に割り当てます。これは、すべてのPythonオブジェクトと単純名の割り当てのために同じ動作です:

In [18]: rows = [[1,2,3],[4,5,6],[7,8,9]] 

In [19]: print(rows) 
[[1, 2, 3], [4, 5, 6], [7, 8, 9]] 

In [20]: for row in rows: 
    ...:  row = ['look','at','me'] 
    ...: 

In [21]: print(rows) 
[[1, 2, 3], [4, 5, 6], [7, 8, 9]] 

実際にあなたがミューテータメソッドを使用する必要が基礎となるデータ構造を変更するには:スライス割り当てがちょうどであることを

In [22]: rows 
Out[22]: [[1, 2, 3], [4, 5, 6], [7, 8, 9]] 

In [23]: for row in rows: 
    ...:  row.append("LOOKATME") 
    ...: 

In [24]: rows 
Out[24]: [[1, 2, 3, 'LOOKATME'], [4, 5, 6, 'LOOKATME'], [7, 8, 9, 'LOOKATME']] 

注意をミューテータメソッドの構文糖:

In [26]: rows 
Out[26]: [[1, 2, 3], [4, 5, 6], [7, 8, 9]] 

In [27]: for row in rows: 
    ...:  row[:] = ['look','at','me'] 
    ...: 
    ...: 

In [28]: rows 
Out[28]: [['look', 'at', 'me'], ['look', 'at', 'me'], ['look', 'at', 'me']] 

これはpandaslocまたはアッシベースilocに似ています構成。

+0

おかげJuanpa。 df.apply(...)help しかし、私の出力はnew_dfであり、dfではありません。 私はそれをより正確にすべきでした。とにかくおかげで :) – murphy1310

+0

?これはあなたの問題を解決しません。私はデータフレームdfと呼んだ。 –

+0

が必要な場合は、結果をnew_dfに追加できます。私はそれを意味しませんでした:) df.apply()は問題を解決します.. ありがとう! 私の問題は根底にあるデータを変更することではないということでした。多分私はあまり明確ではなかったかもしれません。 – murphy1310

関連する問題