मुझे डेटाफ़्रेम के लिए सभी पंक्तियों को एक कॉलम में एक पंक्ति में संयोजित करने की आवश्यकता है। उदाहरण के लिए:

data = ('Hello', 'are you having','', 
        'a nice day today? I am very', 
        'delighted to have snail as','',
        'my','','entree for dinner tonight.')

data = pd.DataFrame(list(data))

रिटर्न:

print(data)
                             0
0                        Hello
1               are you having
2                             
3  a nice day today? I am very
4     delighted to have snail as
5                             
6                           my
7                             
8   entree for dinner tonight.

अब मैं सभी पंक्तियों को एक कॉलम में संयोजित करने का प्रयास करूँगा:

data = data.rename(columns={0: 'value'})
data = data.groupby('value').apply(' '.join).reset_index()

यह प्रत्येक पंक्ति के लिए स्ट्रिंग "मान" के साथ एक अतिरिक्त कॉलम बनाएगा। किसी भी सहायता की सराहना की जाएगी।

0
Starbucks 31 जुलाई 2019, 00:06

1 उत्तर

सबसे बढ़िया उत्तर

आशा है कि आप यही चाहते थे।

import pandas as pd
data = ('Hello', 'are you having','', 
        'a nice day today? I am very', 
        'delighted to have snail as','',
        'my','','entree for dinner tonight.')

data = pd.DataFrame(list(data))
data = data.rename(columns={0: 'value'})
data['new'] = data['value'].str.cat(sep=' ')#concatenate all the strings in existing column and create a new one.
print(data)

आउटपुट:


                                                 new  
0  Hello are you having  a nice day today? I am v...  
1  Hello are you having  a nice day today? I am v...  
2  Hello are you having  a nice day today? I am v...  
3  Hello are you having  a nice day today? I am v...  
4  Hello are you having  a nice day today? I am v...  
5  Hello are you having  a nice day today? I am v...  
6  Hello are you having  a nice day today? I am v...  
7  Hello are you having  a nice day today? I am v...  
8  Hello are you having  a nice day today? I am v...  
1
LazyCoder 31 जुलाई 2019, 00:16