Loading in dataframes with columns spanning multiple rows using pd.read_clipboard

Given a dataset from another question :

    user                             item  \
0  b80344d063b5ccb3212f76538f3d9e43d87dca9e          The Cove - Jack Johnson   
1  b80344d063b5ccb3212f76538f3d9e43d87dca9e  Entre Dos Aguas - Paco De Lucia   
2  b80344d063b5ccb3212f76538f3d9e43d87dca9e            Stronger - Kanye West   
3  b80344d063b5ccb3212f76538f3d9e43d87dca9e    Constellations - Jack Johnson   
4  b80344d063b5ccb3212f76538f3d9e43d87dca9e      Learn To Fly - Foo Fighters   

rating  
0       1  
1       2  
2       1  
3       1  
4       1 

Is there a way to load such data in a given format without having to manually move everything on one line?

+4
source share
1 answer

One way is to compile based on \n\nand then create separate data frames and then concatenate them. iee

#Bit of code from https://stackoverflow.com/questions/45740537/copying-multiindex-dataframes-with-pd-read-clipboard

def read_clipboard_split(index_names_row=None, **kwargs):
    encoding = kwargs.pop('encoding', 'utf-8')

    # only utf-8 is valid for passed value because that what clipboard
    # supports
    if encoding is not None and encoding.lower().replace('-', '') != 'utf8':
        raise NotImplementedError(
            'reading from clipboard only supports utf-8 encoding')

    from pandas import compat, read_fwf
    from pandas.io.clipboard import clipboard_get
    from pandas.io.common import StringIO

    data = clipboard_get()
    items = data.split("\n\n")
    k = []
    for i in items:
        k.append(read_fwf(StringIO(i), **kwargs))
    df = pd.concat(k,axis=1)
    return df

read_clipboard_split()

Run Example:

     user \      
0 b80344d063b5ccb3212f76538f3d9e43d87dca9e
1 b80344d063b5ccb3212f76538f3d9e43d87dca9e  
2 b80344d063b5ccb3212f76538f3d9e43d87dca9e   
3  b80344d063b5ccb3212f76538f3d9e43d87dca9e   
4  b80344d063b5ccb3212f76538f3d9e43d87dca9e   

   rating  
0       1  
1       2  
2       1  
3       1  
4       1 

:

   Unnamed: 0              user                       \  Unnamed: 0  rating
0  0           b80344d063b5ccb3212f76538f3d9e43d87dca9e  0           1     
1  1           b80344d063b5ccb3212f76538f3d9e43d87dca9e  1           2     
2  2           b80344d063b5ccb3212f76538f3d9e43d87dca9e  2           1     
3  3           b80344d063b5ccb3212f76538f3d9e43d87dca9e  3           1     
4  4           b80344d063b5ccb3212f76538f3d9e43d87dca9e  4           1     
+1

Source: https://habr.com/ru/post/1684471/


All Articles