0

I want to create a dataframe from a list, the thing is that my column name is also in the list.

List:

['Input_file_column_name,Is_key,Config_file_column_name,Value\nEmployee ID,Y,identifierValue,identityTypeCode:001\nCumb ID,N,identifierValue,identityTypeCode:002\nFirst Name,N,first_Name \nLast Name,N,last_Name   \nEmail,N,email_Address   \nEntityID,N,entity_Id,entity_Id:01\nSourceCode,N,sourceCode,sourceCode:AHRWB\n']

Resulting dataframe:

Input_file_column_name Is_key Config_file_column_name                 Value
0            Employee ID      Y         identifierValue  identityTypeCode:001
1                Cumb ID      N         identifierValue  identityTypeCode:002
5               EntityID      N               entity_Id          entity_Id:01
6             SourceCode      N              sourceCode      sourceCode:AHRWB

How do I convert it? Do I convert the list to a dictionary and then do it or is there a way that it can be done directly?

Code:

import pandas as pd
with open('onboard_config.txt') as myFile:
  text = myFile.read()
result = text.split("regex")
print result 

df=pd.DataFrame[[sub.split(",") for sub in result]]
2
  • 1
    Your data is a list of a single string, is that the intended input format? Commented Dec 7, 2018 at 18:09
  • No, it is not. The file is in a comma separated form, but I need some other text from the file as well which should not be in the dataframe. Question updated with code. Commented Dec 7, 2018 at 18:11

2 Answers 2

2

Seems like you need splitlines then convert to Series.str.split

df=pd.Series(l[0].splitlines()).str.split(',',expand=True).T.set_index(0).T.dropna()
df
Out[1183]: 
0 Input_file_column_name          ...                          Value
1            Employee ID          ...           identityTypeCode:001
2                Cumb ID          ...           identityTypeCode:002
6               EntityID          ...                   entity_Id:01
7             SourceCode          ...               sourceCode:AHRWB
[4 rows x 4 columns]
Sign up to request clarification or add additional context in comments.

4 Comments

What if I do not want to remove "na" values?
@ManasJani df=pd.Series(l[0].splitlines()).str.split(',',expand=True).T.set_index(0).T
How do I not have the header column with index=0?
@ManasJani del df.columns.name
0
    split=list[0].split('\n')
    df= []
    for i in split:
        df.append(i.split(','))

    columns= df[0]
    df=df[1:]
    pd.DataFrame(df, columns=columns)

This will give you your desired df.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.