0

I have posted this question earlier.

At the output of this program, I get an array that has 4 elements like this:

11111111,22222222,kkkkkk,lllllll
33333333,44444444,oooooo,ppppppp
qqqqqqqq,rrrrrr,ssssss,ttttttt

Now I have another csv which has more columns(let's say 10) and a some of those columns have hardcoded data, something like this -

head1,head2,head3,head4,head5,head6,head7,head8,head9,head10
-,123,345,something,<blank>,<blank>,-,-,-,-

so except for the everything is hardcoded. I want to print the first and second columns of my output in these blank spaces and repeat the hardcoded data on every row. So my output should be something like this:

head1,head2,head3,head4,head5,head6,head7,head8,head9,head10
-,123,345,something,11111111,22222222,-,-,-,-
-,123,345,something,33333333,44444444,-,-,-,-
-,123,345,something,qqqqqqqq,rrrrrr,-,-,-,-
5
  • 3
    can you share what you have tried already Commented Jan 30, 2019 at 5:55
  • @AmitNanaware in the code that I have shared I have loaded the contents into an array.And the code that i have tried so far can dump into a new csv but I have no idea how to dump the array into an existing csv with hardcoded data Commented Jan 30, 2019 at 6:05
  • 1
    Iterate over to look for the empty columns, if found, dump it with the desired data from the array. Ta-daah! Commented Jan 30, 2019 at 6:06
  • @user5173426 thanks trying it out. what i am trying was load both the files in 2d arrays and then iterating over the files and assigning values Commented Jan 30, 2019 at 6:13
  • 1
    you can iterate from 2nd line and you can split by , and your data in that array Commented Jan 30, 2019 at 6:18

1 Answer 1

1

Approach:

1) Read lines from the done.csv and append them to separate lists.

2) Read the new csv with empty column data, Lets call it missing_data.csv

3) Iterate for the number of lists in 1) i.e. 3 in your case.

4) Iterate over each column of missing_data.csv until an empty value is found

5) Fill the empty column with the list currently running from 3)

Hence:

1):

import pandas as pd

initial_data1 = []
initial_data2 = []
initial_data3 = []

    line_num = 1
with open ("list.txt") as f:
    content = f.readlines()
    for line in content:
        if line_num == 1:
            initial_data1.append(line.split(","))
        elif line_num == 2:
            initial_data2.append(line.split(","))
        elif line_num == 3:
            initial_data3.append(line.split(","))

        line_num = line_num + 1

print(initial_data1)
print(initial_data2)
print(initial_data3)

OUTPUT:

[['11111111', '22222222', 'kkkkkk', 'lllllll\n']]
[['33333333', '44444444', 'oooooo', 'ppppppp\n']]
[['qqqqqqqq', 'rrrrrr', 'ssssss', 'ttttttt']]

The rest:

df = pd.read_csv("missing_data.csv")
heads = ['head1','head2','head3','head4','head5','head6','head7','head8','head9','head10']

   appending_line = 0
for index, row in df.iterrows():    
        if appending_line == 0:
            initial_data = initial_data1
        elif appending_line == 1:
            initial_data = initial_data2
        elif appending_line == 2:
            initial_data = initial_data3
        j = 0
        k = 0
        appending_line += 1
        for i in range(0, len(heads)):   # for the number of heads
                if str(row[heads[i]]) == " ":
                    print("FOUND EMPTY COLUMN: ", heads[i])
                    print("APPENDING VALUE: ", initial_data[j][k])
                    row[heads[i]] = initial_data[j][k]
                    k += 1

OUTPUT:

FOUND EMPTY COLUMN VALUE:  head5
APPENDING VALUE:  11111111
FOUND EMPTY COLUMN VALUE:  head6
APPENDING VALUE:  22222222
FOUND EMPTY COLUMN VALUE:  head5
APPENDING VALUE:  33333333
FOUND EMPTY COLUMN VALUE:  head6
APPENDING VALUE:  44444444
FOUND EMPTY COLUMN VALUE:  head5
APPENDING VALUE:  qqqqqqqq
FOUND EMPTY COLUMN VALUE:  head6
APPENDING VALUE:  rrrrrr
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.