2016-11-11 3 views
3

ich folgende Datenrahmen haben:Wie komprimiere ich einen multidimensionalen Datenrahmen in eine einzelne Spalte?

0 1 2 3 4 5 6 7 8 
0 Twitter (True 01/21/2015) None None None None None None None None 
1 Google, Inc. (True 11/07/2016) None None None None None None None None 
2 Microsoft, (True 07/01/2016) Facebook (True 11/01/2016) None None None None None None None 
3 standard & poors, Inc. (True 11/08/2016) None None None None None None None None 
8 apple (True 11/10/2016)  apple (True 11/01/2016)  None None None None None apple (True 11/01/2016)  None 

Wie kann ich den oben Datenrahmen in einem einzigen Datenrahmen zu komprimieren ?:

0 
0 Twitter (True 01/21/2015) 
1 Google, Inc. (True 11/07/2016) 
2 Microsoft, (True 07/01/2016) \ Facebook (True 11/01/2016) 
3 standard & poors, Inc. (True 11/08/2016) \ 
8 apple (True 11/10/2016) \ apple (True 11/01/2016) \ apple (True 11/01/2016) 

ich versucht:

df = df.iloc[:,0].join('\') 

Aber ich tun verstehe nicht, wie man ein Trennzeichen hinzufügt. Wie sollte ich den Datenrahmen mit einem Trennzeichen komprimieren ?.

Antwort

3

Ich glaube, Sie brauchen replaceNone-NaN und dann NaN von stack, entfernen letzte groupby mit applyjoin:

df = df.replace({None: np.nan, 'None': np.nan}).stack() 
df = df.groupby(level=0).apply(' \\ '.join) 
print (df) 
0       Twitter (True 01/21/2015) 
1      Google, Inc. (True 11/07/2016) 
2 Microsoft, (True 07/01/2016) \ Facebook (True ... 
3    standard & poors, Inc. (True 11/08/2016) 
8 apple (True 11/10/2016) \ apple (True 11/01/20... 
dtype: object 

Eine andere Lösung mit Liste Verständnis:

df = df.replace({None: np.nan, 'None': np.nan}) 
#python 3 use str, python 2 basestring 
df = df.apply(lambda x : ' \\ '.join([y for y in x if isinstance(y, str)]), axis=1) 

print (df) 
0       Twitter (True 01/21/2015) 
1      Google, Inc. (True 11/07/2016) 
2 Microsoft, (True 07/01/2016) \ Facebook (True ... 
3    standard & poors, Inc. (True 11/08/2016) 
8 apple (True 11/10/2016) \ apple (True 11/01/20... 
dtype: object 

Timings:

#[50000 rows x 9 columns] 
df = pd.concat([df]*10000).reset_index(drop=True) 

In [43]: %timeit (df.replace({None: np.nan, 'None': np.nan}).apply(lambda x : ''.join([y for y in x if isinstance(y, str)]), axis=1)) 
1 loop, best of 3: 820 ms per loop 

In [44]: %timeit (df.replace({None: np.nan, 'None': np.nan}).stack().groupby(level=0).apply(' \\ '.join)) 
1 loop, best of 3: 4.62 s per loop 
+0

Danke, aber ich habe eine seltsame Format. Jedes Zeichen geht unter. – student

+0

Was ist schneller? ... Ich habe festgestellt, dass die erste Lösung ein wenig dauert – student

+1

siehe Zeiten in Update-Lösung – jezrael

0

Sie können versuchen, diese (ich die folgende Ausgabe mit einem kleinen Datenrahmen erhalten, das in Ordnung scheint):

df = pd.DataFrame({'0':['Twitter (True 01/21/2015)', 'Google, Inc. (True 11/07/2016)', ' Microsoft, (True 07/01/2016)'], '1':[None, None, 'Facebook (True 11/01/2016)'], '2':[None, None, None]}) 
df = df.replace({None: ' ', 'None': ' '}) 
df.astype(str).apply(lambda x: '\\'.join(x), axis=1) 


0      Twitter (True 01/21/2015)\ \ 
1     Google, Inc. (True 11/07/2016)\ \ 
2  Microsoft, (True 07/01/2016)\Facebook (True ... 
dtype: object 
+0

Danke, ich habe ein komisches Format. Alle Charaktere sind zersplittert. – student

+0

Wie sieht Ihre Ausgabe aus? –

+0

'l ik e t h i s' – student

Verwandte Themen