Home > Enterprise >  Concatenating 2 csv into 1 using pandas python
Concatenating 2 csv into 1 using pandas python

Time:10-11

// I am trying to concatenate my 2 csv files into pandas data frame but I am not getting proper output 1st csv contains only 3 columns named ProductID ProductName ProductBrand and 2 nd csv contains this columns Price (INR) NumImages ID Description where instead of data I am getting Nan I am getting data in my 2nd csv only after data of my 1 st csv is getting finished I am new to this Any help would be highly appreciated I am sharing my code too

// output:


    ProductID   ProductName ProductBrand    Gender  Price (INR) NumImages   ID  Description PrimaryColor
    10017413.0  DKNY Unisex Black   DKNY    NaN NaN NaN NaN NaN NaN
    10016283.0  EthnoVogue Women    EthnoVogue  NaN NaN NaN NaN NaN NaN

// Python Code:

import pandas as pd

# merging two csv files
df = pd.concat(map(pd.read_csv, ['data1.csv', 'data2.csv']), ignore_index=True)

CodePudding user response:

Add axis=1 as below.

df = pd.concat(map(pd.read_csv, ['a.csv', 'b.csv']), ignore_index=True, axis = 1)

CodePudding user response:

Maybe is your mapping that is not working properly. I suggest 2 ways:

Given your .csv list: csv_list = ['data1.csv', 'data2.csv']

Using map:

df_mapper = map(lambda f: pd.read_csv(f), csv_list)
df = pd.concat(list(df_mapper), ignore_index=True)

Using list comprehension:

df_list = [pd.read_csv(f) for f in csv_list]
df = pd.concat(df_list, ignore_index=True)
  • Related