2016-12-13 34 views
5

tüm hücre değerlerinden önek/eki ​​kaldırmak, ben genellikle,pandalar dataframe: eklemek ve bir dataframe için bir önek/sonek eklemek için tüm dataframe

bir son ek '@' eklemek Örneğin

.. aşağıdakileri yapın

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

Bu temelde tüm cep değerlere '@' eklenmiş oldu.

Bu son ekin nasıl kaldırılacağını öğrenmek istiyorum. pandas.DataFrame sınıfı ile doğrudan tüm DataFrame'in belirli bir önek/sonek karakterini kaldıran bir yöntem var mı? ,

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

Ancak

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

Şimdi, bu serinin dışarı dataframe yapmak için,: rstrip('@') kullanarak aşağıdaki gibi iken

I (dizi olarak) satır yineleme denedim Bu işe yaramıyor. Boş veriye verir.

Eksik olduğum temel bir şey var mı?

Sen apply kullanabilirsiniz

cevap

3

Her elemana dize yöntemini uygulamak için applymap kullanabilirsiniz:

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

Mükemmel! Teşekkürler Alex :) – murphy1310

5

ve pd.Series ait str.strip yöntemi:

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 

Not yaklaşımınız çalışmaz çünkü şu atama yaptığınızda sizin için-döngü:

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

Bu, DataFrame'u mutasyona uğratmadan sonucunu row adına atar.

aslında bir Mutator yöntemi kullanmak gerekir temel veri yapısını değiştirmek için

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']] 

Not o dilim-atama adildir Bu, tüm piton nesneler ve basit isim ataması için aynı davranıştır bir mutatör yöntemi için sözdizimsel şeker:

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']] 

Bu pandasloc veya iloc göre Assi benzerdir gnment.

+0

Teşekkür Juanpa. df.apply (...), yardımcı olur. Ancak, çıkışım new_df ve df değil. Bununla daha hassas olmalıydım. Teşekkürler :) – murphy1310

+0

Huh? Bu probleminizi çözmez. Veri çerçevemi aradım df. –

+0

oh no'yu isterseniz, sonucu new_df'ye ekleyebilirsiniz. Bu demek istemedim :) df.apply() sorunu çözüyor .. Teşekkürler! Sorunun temel verileri mutasyona uğratmakla ilgili olmadığını kastediyorum. Belki de çok açık değildim. – murphy1310