0

I want to insert values from the list based on the condition,

For example, Please find below my algorithm of the code using CSV module.

out_file = open("c://Project//in.csv", "w")
header = ['List','Integer', 'Float', 'String']
l = [[4,5], 12, 2.4, "This is a string", [1,2], 45, ["Second String"]]
writer = csv.writer(out_file)
insert_header_in_the_beginning
for i in l:
    if check_first_element_is_list_using_regex:
        insert_in_first_column_of_first_row(i)
    elif check_second_element_is_integer_using_regex:
        insert_in_second_column_of_first_row(i)
    elif check_third_element_is_float_using_regex:
        insert_in_third_column_of_first_row(i)
    elif check_last_element_is_string_using_regex:
        insert_in_last_column_of_first_row(i)
    new_row_starts

Desired Output

List    Integer    Float    String
[4,5]    12         2.4     This is a String
[1,2]    45         None    Second String
.....        
2
  • I realise this is pseudocode but I don't understand the apparent intention to use RE for analysing the content of the list. Can you explain that? Also, please show what your expected output (the CSV file content) should look like given your sample data Commented Oct 21, 2021 at 6:47
  • @BrutusForcus Used RE to recognize the element whether it is Char, Int, or Decimal. Actually here I just shown the simplest example, however the list in the real code having a complex pattern. And Yes, I have updated the desired output in my question. Thanks.! Commented Oct 21, 2021 at 6:53

1 Answer 1

1

A loop isn't a great idea. You have two better choices : You can use np.select with two list of conditions, values like this :

condlist = [x<3, x>5]
choicelist = [x, x**2]
np.select(condlist, choicelist)

To make it easy with your problem :

import pandas as pd

liste_of_Liste = []
liste_of_Integer = []
liste_of_Float = []
liste_of_String = []
l = [[4,5], 12, 2.4, "This is a string", [1,2], 45, ["Second String"]]
for i in l:
    if type(i) is list:
        liste_of_Liste.append(i)
    elif type(i) is int:
        liste_of_Integer.append(i)
    elif type(i) is float:
        liste_of_Float.append(i)
    else:
        liste_of_String.append(i)

Then your arrays don't have the same length, i made it in the rows but you can fill it with NaN values if you want :

your_dictionnary = {
    'List'    : liste_of_Liste,
    'Integer' : liste_of_Integer,
    'Float'   : liste_of_Float,
    'String'  : liste_of_String
    }

df = pd.DataFrame.from_dict(your_dictionnary, orient="index")
print(df)
Sign up to request clarification or add additional context in comments.

2 Comments

Hi, Thanks for the response. Can you provide me the solution based on the data I have mentioned in the question ?
I edited with a simplier way, say me if it's ok

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.