Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Merge 2 csv files rows

Tags:

python

pandas

csv

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.

like image 622
Seles Calin Avatar asked Mar 15 '26 18:03

Seles Calin


1 Answers

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.

like image 85
Valdi_Bo Avatar answered Mar 17 '26 12:03

Valdi_Bo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!