Seles Calin
Seles Calin

Reputation: 33

Merge 2 csv files rows

So i'm trying to predict the winner of a sport game, and i have 2 CSV files. One with the current year statistics and the other with last years statistics.

I would like to merge them but only with the colums from the first file:

So that if the first table has columns ['Away','Home','Result']

and the second one has ['Away','Home','Match-Rating']

the result would contain ['Away','Home','Result'] and the 'Result' column would contain 0 or other default value if not found in second CSV.

I tried :

data = panda.read_csv('PremierLeagueDataSet/19-20.csv')
display(data.head())
data2= panda.read_csv('PremierLeagueDataSet/18-19.csv')
data.append(data2)

but gives me a warning and doesn't do the wanted concatenation

FutureWarning: Sorting because non-concatenation axis is not aligned. A future version of pandas will change to not sort by default.

Upvotes: 3

Views: 115

Answers (1)

Valdi_Bo
Valdi_Bo

Reputation: 30981

To block data2.Match-Rating from appending, invoke append passing data2 with column names to be included:

data.append(data2[['Away', 'Home']], ignore_index=True, sort=False)\
    .replace(np.nan, '')

As you can see, I added ignore_index=True to avoid repeating indices. I added also sort=False to avoid a warning concerning planned changes in future versions.

I added also replace to change NaN values into empty strings.

Upvotes: 2

Related Questions