2

I have two .csv files named all_cv.csv and common_cv.csv files. First I have concat this two csv files by pandas, and then save the data into a new file named join_cv_common.csv by pandas. After that I sorted the join_cv_common.csv file by pandas as below, and stored data are stored into a new file named sorted_cv_common.csv. I want to rewrite these two functions of pandas - concat and sort by pure python (2.6 and 3.4). Can someone help me in this regards? Thank you very much.

Pandas concat function

cv = pd.read_csv('all_cv.csv')

ac = pd.read_csv('common_cv.csv')

merged = pd.concat([cv, ac])

merged.to_csv('join_cv_common.csv')

Pandas sorting function

df = pd.read_csv('join_cv_common.csv')

df = df.sort(["adv_id", "conv_id"])

df.to_csv('sorted_cv_common.csv')
2
  • Any reason you don't sort prior to writing to csv? Commented Nov 26, 2015 at 9:00
  • Let me clarify: do you want to "rewrite pandas.concat and pandas.sort in pure Python"? What is preventing you from using the implementations within pandas? Thanks. Commented Nov 26, 2015 at 9:30

1 Answer 1

2

Using the file i/o and list sorting

By my knowledge it can be done by reading both the files using the file i/o after that just join and convert the string to a list after that sort the newly created list and put it in the final output csv by converting the list to a string. Following is the code implementation for that.

123.csv

1,a

2,b

4,d

456.csv

3,c

5,d

Read csv file in d1 and d2 using file open function

d = d1 + '\n' +  d2
lst = d.split('\n')
data = "\n".join(sorted(lst))
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.