简体   繁体   中英

reading nested lists from csv to pandas

I am trading a csv file row-wise:

for n in range(len(df_config)):
    init_list = df_config.at[n,'init_list'].split(",")
    init_list = list(map(float, init_list)) 

When I read a cell (init_list) from csv file it is read as follows:

'[1, 1, 1, 1, 1, 1, 1, 1],[.2, .2, .2, .2, .2, .2, .2, .2],[.8, .7, .7, .7, .8, .8, .8, .8],[1, 1, 1, 1, 1, 1, 1, 1]'

This is in string format.

I need to be able to read each individual list as per the index number. I am trying something like this:

    for idx,item in enumerate(init_list):
        df_templete[item] = init_list[idx]

I am looking to read the above list like:

idx = 0, then item = [1, 1, 1, 1, 1, 1, 1, 1]
idx = 1, then item = [.2, .2, .2, .2, .2, .2, .2, .2]
idx = 2, then item = [.8, .7, .7, .7, .8, .8, .8, .8]
idx = 3, then item = [1, 1, 1, 1, 1, 1, 1, 1]

Additional Information:

My input dataframe is as follows:

df_templete
Categories  AA  BB  CC  DD
Rock
Paper
Scissors
Lizard
Spock

I am looking to create the below dataframe:

Categories  AA  BB  CC  DD  Increse_AA  Increse_BB  Increse_CC  Increse_DD
Rock                            1           0.2       0.8          1
Paper                           1           0.2       0.8          1
Sissors                         1           0.2       0.7          1
Lizard                          1           0.2       0.7          1
Spock                           1           0.2       0.8          1

All these columns and rows come from same csv type file. with the above enumarate function, I was able to populate the values of increse columns incase they were same for all categories of one country but now since I have to pooulate individual values for category with respect to each column ( AA, BB, CC, DD ) I am trying to figure out a way to do this.

This is the first row of the csv file which i am using as an input to form the above dataframe:

Categories                  Country              init_list
Rock, Paper, Scissors,      AA, BB, CC, DD      [1, 1, 1, 1, 1, 1, 1, 1],
Lizard, Spock                                   [.2, .2, .2, .2, .2, .2, .2, .2],
                                                [.8, .7, .7, .7, .8, .8, .8, .8],
                                                [1, 1, 1, 1, 1, 1, 1, 1]

which can be generated like so:

from io import StringIO
import pandas as pd

data = """\
Categories                  Country              init_list
Rock,Paper,Scissors,Lizard,Spock      AA,BB,CC,DD      [1, 1, 1, 1, 1, 1, 1, 1],[.2, .2, .2, .2, .2, .2, .2, .2],[.8, .7, .7, .7, .8, .8, .8, .8],[1, 1, 1, 1, 1, 1, 1, 1]
"""

df = pd.read_csv(StringIO(data), sep="\s\s+", engine="python")

use eval if your csv file is trustly

s = '[1, 1, 1, 1, 1, 1, 1, 1],[.2, .2, .2, .2, .2, .2, .2, .2],[.8, .7, .7, .7, .8, .8, .8, .8],[1, 1, 1, 1, 1, 1, 1, 1]'
eval(s)

gets

([1, 1, 1, 1, 1, 1, 1, 1], [0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2], [0.8, 0.7, 0.7, 0.7, 0.8, 0.8, 0.8, 0.8], [1, 1, 1, 1, 1, 1, 1, 1])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM