Search code examples
pythonpandasexcelpycharm

assigning values separated by a comma in a column to different columns using python


In the original dataframe, there is a column named 'NVE Liste' and only has one row. In that column, the values are '0034104060001008405,00341040600001008498,00341040600002187444,00341040600002187505,00341040600002187512,00341040600002190079,00341040600002190093,00341040600002196880,00341040600012187434,00341040600012187496'. Now firstly i want to split every number separted by comma in a different column. for example, the first number should be in column named 'NVE1', the second number should be in column named 'NVE2' and so on, in the new dataframe.

how can i achieve this any help would be appreciated

i tried using str.split, also tried different methods but it ended up not making new columns it just made an extra with same values


Solution

  • You can do it the same way you would add a calculated column but use str.split(','). Here is one way it could be accomplished using the sample data you provided.

    import pandas as pd
    
    #Create a sample dataframe per the OP question
    entrylist=[]
    OriginalDF=pd.DataFrame(columns = [['NVEListe']])
    #You can add more entries than just one to the list if you want a better example
    entrylist.append('0034104060001008405,00341040600001008498,00341040600002187444,00341040600002187505,00341040600002187512,00341040600002190079,00341040600002190093,00341040600002196880,00341040600012187434,00341040600012187496')
    
    OriginalDF.loc[0]=entrylist
    print('This is the original DataFrame')
    display(OriginalDF)
    
    #split the original entry using the comma separator
    splitentry=(OriginalDF.iloc[0]['NVEListe']).split(',')
    
    #Make a list of names for the new columns
    NewColumnlst=[]
    for i in range(len(splitentry)):
        NewColumnlst.append('NVEListe'+str(i+1))
    
    #Split the string and add to new columns
    for i in range(len(splitentry)):
        OriginalDF[NewColumnlst[i]]=splitentry[i]
    
    #Drop the unsplit original column
    OriginalDF.drop('NVEListe', axis=1, inplace=True)
    
    
    print('This is the new DataFrame showing the split columns')
    display(OriginalDF)
    

    enter image description here