1

I have two CSV files, and I want to combine them in one file. My first CSV file is called vector_train.csv and the second is label_train.csv

vector_train.csv
v1,v2,v3,v100
12,32,15,30
55,72,45,90

label_train.csv
sentence,label
bad voice,0
good voice,1

I want the output like this

vector_train.csv
v1,v2,v3,v100,label
12,32,15,30,0
55,72,45,90,1

please help me

3 Answers3

0

You can use pandas dataframe to read the csv files https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.read_csv.html

and then combine the two files as one dataframe https://stackoverflow.com/questions/12850345/how-to-combine-two-data-frames-in-python-pandas

and then save in a csv file https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.to_csv.html

0

You have said how you join the 2 data frames. I assume you want to join them line by line:

vector = pd.read_csv('vector_train.csv')
label = pd.read_csv('label_train.csv')

vector['label'] = label['label']
vector.head()

Output:

v1  v2  v3  v100  label
12  32  15  30    0
55  72  45  90    1
Code Different
  • 90,614
  • 16
  • 144
  • 163
0

You can import both csv into 2 different dataframe and then you can add an extra column to the first dataframe and assign it equal to the column of second dataframe as:

import pandas as pd
vector_train_df = pd.read_csv("vector_train.csv")
label_train_df = pd.read_csv("label_train.csv")
vector_train_df['label'] = label_train_df['label'] 
Ankit Agrawal
  • 616
  • 9
  • 20