0

So I was trying to split a list of values into dataframe in Python.

Here is a sample example of my list

    ini_string1 = "Time=2014-11-07 00:00:00,strangeness=0.0001,p-value=0.19,deviation=0.78,D_Range=low'"
    templist = []  
    for i in range(5):
        templist.append({ini_string1})

Now I was trying to create a dataframe with Time, Strangeness, P-Values, Deviation, D_Range as variables.

I was able to get a data frame when I have only one sigle value of ini_string but counld not make it when I have list of values.

Below is a sample code I tried with single value ini_string

lst_dict = []

cols = ['Time','Strangeness', 'P-Values', 'Deviation', 'Is_Deviation']
# Initialising string 

for i in range(5):
    ini_string1 = "Time=2014-11-07 00:00:00,strangeness=0.0001,p-value=0.19,deviation=0.78,D_Range=low'"

tempstr = ini_string1 
res = dict(item.split("=") for item in tempstr.split(","))
lst_dict.append({'Time': res['Time'],
                 'Strangeness': res['strangeness'],
                 'P-Values': res['p-value'],
                 'Deviation': res['deviation'],
                 'Is_Deviation': res['D_Range']})

print(lst_dict)

strdf = pd.DataFrame(lst_dict, columns=cols)

I could not figureout the implementation for list of values

0

1 Answer 1

3

The below code will do the job.

from collections import defaultdict
import pandas as pd

ini_string1 = "Time=2014-11-07 00:00:00,strangeness=0.0001,p-value=0.19,deviation=0.78,D_Range='low'"
ini_string2 = "Time=2015-12-07 00:00:00,strangeness=0.0005,p-value=0.31,deviation=0.01,D_Range='high'"

ini_strings = [ini_string1, ini_string2] 

dd = defaultdict(list)
for ini_str in ini_strings:
    for key_val in ini_str.split(','):
        k, v = key_val.split('=')
        dd[k].append(v)

df = pd.DataFrame(dd)

Read more about defaultdict - How does collections.defaultdict work?
Python has other interesting data structures - https://docs.python.org/2/library/collections.html

Sign up to request clarification or add additional context in comments.

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.