Skip to content

Commit 89e8fd4

Browse files
author
Jayesh Manani
authored
Main Python File
This file consists of the code for merging two CSV file into one and generate a new file into the local repository.
1 parent 9a339f1 commit 89e8fd4

1 file changed

Lines changed: 35 additions & 0 deletions

File tree

Merge2CSV.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#First Import pandas library as pd for ease of use
2+
import pandas as pd
3+
4+
#Now read CSV file from your local location by using read_csv function of pandas library and save
5+
# it to some variable here its df0
6+
df0 = pd.read_csv('CSV/ratings.csv')
7+
8+
#Now read another CSV file from your local location by using read_csv function of pandas library
9+
# and save it to some variable here its df1 and give it encoding='latin-1' only if it can't
10+
# decode UTF-8 encoding
11+
df1= pd.read_csv('CSV/movies.csv',encoding='latin-1')
12+
13+
#Use .head() function of pandas only if you want to see some data in the variable of type dataframe
14+
# by default it takes value of first 5 but you can give some parameter to the function
15+
# .head(10) will shows first 10 values from the dataframe
16+
17+
#print(df0.head())
18+
#print(df1.head())
19+
20+
# use the merge function to merge csv files and give it parameter as shows below, on means the name of the column which
21+
# is common on both csv files and can be used to jion both csv files data into one. and then save it to some variable
22+
result = pd.merge(df1,
23+
df0[['userId', 'movieId', 'rating','timestamp']],
24+
on='movieId')
25+
26+
#print(result.head())
27+
28+
# .shape() function is used for know the shape of the data you are using, like the no. of columns and rows.
29+
#print(df1.shape)
30+
#print(df0.shape)
31+
32+
#print(result.shape)
33+
34+
# use the function .to_csv of the pandas library for saving the result of merged csv files to the new csv files.
35+
result.to_csv('CSV/merged.csv')

0 commit comments

Comments
 (0)