1

I have one csv file like this

out.csv
seedProductId,relatedProducts

My output is in dftype object looks like this

100A7E54111FB143    
100D11CF822BBBDB    
1014120EE9CCB1E0    
10276825CD5B4A26    
10364F56076B46B7    
103D1DDAD3064A66    
103F4F66EEB54308    

I want to append this output with delimiter '|'.And I will pass another input as dynamic.

so I want the op like this

out.csv
seedProductId, relatedProducts
103F4F66EEB54308, 100A7E54111FB143 | 100D11CF822BBBDB | 10276825CD5B4A26 ...

Any help will be appreciated.

7
  • Why seedProductId is 103F4F66EEB54308? Because last item in output? Commented Jan 30, 2016 at 11:44
  • no I just mentioned for example purpose.I will pass the seedproduct id at run time Commented Jan 30, 2016 at 11:49
  • So you need only convert your output from many rows to one row separated by |? Commented Jan 30, 2016 at 11:54
  • yes.and then I want to append into my csv file Commented Jan 30, 2016 at 11:58
  • out.csv is new file or contains data before appending? Commented Jan 30, 2016 at 12:06

1 Answer 1

1

You can use cat for merging data, then create new Series for each columns and concat them. Last write output dataframe to file by to_csv:

print df
                  A
0  100A7E54111FB143
1  100D11CF822BBBDB
2  1014120EE9CCB1E0
3  10276825CD5B4A26
4  10364F56076B46B7
5  103D1DDAD3064A66
6  103F4F66EEB54308

seedProductId = "id790"
s1 = pd.Series(seedProductId, name='seedProductId')
print s1
0    id790
Name: seedProductId, dtype: object

relatedProducts = df.A.str.cat(sep='|')
print relatedProducts
100A7E54111FB143|100D11CF822BBBDB|1014120EE9CCB1E0|10276825CD5B4A26|10364F56076B46B7|103D1DDAD3064A66|103F4F66EEB54308

s2 = pd.Series(relatedProducts, name='relatedProducts')
print s2
0    100A7E54111FB143|100D11CF822BBBDB|1014120EE9CC...
Name: relatedProducts, dtype: object

df = pd.concat([s1, s2], axis=1)

print df.to_csv(index=False)
seedProductId,relatedProducts
id790,100A7E54111FB143|100D11CF822BBBDB|1014120EE9CCB1E0|10276825CD5B4A26|10364F56076B46B7|103D1DDAD3064A66|103F4F66EEB54308
Sign up to request clarification or add additional context in comments.

11 Comments

Ok, I think dftype object is for me dataframe with column A. But what is your dtfype object? Dataframe? Series? Something else?
Yes, you can change it to relatedProducts = output['ProductId'].str.cat(sep='|')
Hi nice.it works for me..I am now trying to append the output in my csv file
the last line is not working for me..any help ? print df.to_csv(index=False)
I think you need add filename df.to_csv("filename.csv", index=False)
|

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.