您如何将数据从数据框架复制到另一个

发布于 2025-01-29 23:00:41 字数 4816 浏览 3 评论 0原文

我很难将正确的数据从参考CSV文件获取正确的数据到我正在处理的文件。

我有一个CSV文件,其中有超过600万行和19列。我看起来像这样: 在此处输入图像描述

对于每一行,都有一个品牌和一个汽车模型,包括其他信息。 我想将每100公里旅行的燃油和所使用的燃料类型添加到此文件中。

我还有另一个CSV文件,它具有每种看起来像这样的汽车模型的燃料消耗:输入图像描述这里

我最终要做的是将g,h,i和j列的匹配值从第二个文件添加到第一个文件。

由于文件的大小,我想知道除了“ for”或“ a”循环之外,还有其他方法可以做其他方法吗?

编辑

例如... 第一个DF看起来像这个

ID品牌模型其他_COLUMNSfuel_consu_1fuel_consu_2
1ToyotaRav4ACNan
NanNanNanNanNanNan
NanNan Nan Nan Nan Nan Nan Nan Nan Nan Nan Nan Nan Nan Nan 3 GmcNanNanNanNan Nan Nan Nan Nan Nan
NanSierraNandNan Nannan d nan

d nan nan spect this

Id idfar fall ful_consu_consu_consu_consu_consu_consu_consu_consu_consu_consu_conu_consu_cod_consu_conu_consu_conu_consu_11nandFuel_consu_2
1ToyotaCorrola100120
2ToyotaRav48084
3GMCSierra91105
4HondaCivic112125

The output should be :

IDBrandModelOther_columnsFuel_consu_1Fuel_consu_2
1ToyotaRav4a8084
2HondaCivicb112125
3GMCSierrac91105
4ToyotaRav4D8084

第一个DF可能具有与不同ID相同的品牌和型号。订单是完全随机的。

I am having a difficult time getting the correct data from a reference csv file to the one I am working on.

I have a csv file that has over 6 million rows and 19 columns. I looks something like this :
enter image description here

For each row there is a brand and a model of a car amongst other information.
I want to add to this file the fuel consumption per 100km traveled and the type of fuel that is used.

I have another csv file that has the fuel consumption of every model of car that looks something like this : enter image description here

What I want to ultimately do is add the matching values of G,H, I and J columns from the second file to the first one.

Because of the size of the file I was wondering if there is another way to do it other than with a "for" or a "while" loop?

EDIT :

For example...
The first df would look something like this

IDBrandModelOther_columnsFuel_consu_1Fuel_consu_2
1ToyotaRav4aNaNNaN
2HondaCivicbNaNNaN
3GMCSierracNaNNaN
4ToyotaRav4dNaNNaN

The second df would be something like this

IDBrandModelFuel_consu_1Fuel_consu_2
1ToyotaCorrola100120
2ToyotaRav48084
3GMCSierra91105
4HondaCivic112125

The output should be :

IDBrandModelOther_columnsFuel_consu_1Fuel_consu_2
1ToyotaRav4a8084
2HondaCivicb112125
3GMCSierrac91105
4ToyotaRav4d8084

The first df may have many times the same brand and model for different ID's. The order is completely random.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

日暮斜阳 2025-02-05 23:00:42

感谢您提供更新,我能够将一些应该能够帮助您的东西放在一起

#You drop these two columns because you won't need them once you join them to df1 (which is your 2nd table provided)
df.drop(['Fuel_consu_1', 'Fuel_consu_2'], axis = 1 , inplace = True)
#This will join your first and second column to each other on the Brand and Model columns 
df_merge = pd.merge(df, df1, on=['Brand', 'Model'])

Thank you for providing updates I was able to put something together that should be able to help you

#You drop these two columns because you won't need them once you join them to df1 (which is your 2nd table provided)
df.drop(['Fuel_consu_1', 'Fuel_consu_2'], axis = 1 , inplace = True)
#This will join your first and second column to each other on the Brand and Model columns 
df_merge = pd.merge(df, df1, on=['Brand', 'Model'])
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文