合并两个CSV文件的行。

huangapple go评论88阅读模式
英文:

Merge 2 csv files rows

问题

我正在尝试预测一场体育比赛的获胜者,我有两个CSV文件。一个包含当前年份的统计数据,另一个包含去年的统计数据。

我想要合并它们,但只使用第一个文件中的列:

所以如果第一个表有列
['Away', 'Home', 'Result']

而第二个表有['Away', 'Home', 'Match-Rating']

结果将包含['Away', 'Home', 'Result'],而'Result'列将包含0或其他默认值(如果在第二个CSV中找不到)。

我尝试了:

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

但是它会给我一个警告,并且不执行所需的连接。

未来警告:排序,因为非连接轴未对齐。pandas的未来版本将默认不进行排序。

英文:

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.

答案1

得分: 2

为了阻止 data2.Match-Rating 的追加,调用 append 并传递包含要包括的列名的 data2

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

正如你所看到的,我添加了 ignore_index=True 来避免重复的索引。我还添加了 sort=False 来避免关于未来版本计划更改的警告。

我还添加了 replace 来将 NaN 值更改为空字符串。

英文:

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.

huangapple
  • 本文由 发表于 2020年1月3日 23:50:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/59581489.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定