简体   繁体   中英

how to concat two csv file and then sort by python

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')

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))

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