简体   繁体   English

如何将 rest 一个行值转换为另一个 dataframe 的第 n 行值

[英]How to rest a row value to the nths rows values of another dataframe

I have this two df's我有这两个df

 df1:
    lon                  lat
 0  -60.7               -2.8333333333333335
 1  -55.983333333333334 -2.4833333333333334
 2  -51.06666666666667  -0.05
 3  -66.96666666666667  -0.11666666666666667
 4  -48.483333333333334 -1.3833333333333333
 5  -54.71666666666667  -2.4333333333333336
 6  -44.233333333333334 -2.6
 7  -59.983333333333334 -3.15

 df2:
      lon          lat  
 0  -24.109     -2.0035
 1  -17.891     -1.70911
 2  -14.5822    -1.7470700000000001
 3  -12.8138    -1.72322
 4  -14.0688    -1.5028700000000002
 5  -13.8406    -1.44416
 6  -12.1292    -0.671266
 7  -13.8406    -0.8824270000000001
 8  -15.12      -18.223

I want to rest each value of df1['lat'] with all values of df2 Something like this:我想 rest df1['lat'] 的每个值和 df2 的所有值是这样的:

results0=df1.loc[0,'lat']-df2.loc[:,'lat']
results1=df1.loc[1,'lat']-df2.loc[:,'lat']
#etc etc....

So i tried this:所以我尝试了这个:

for i,j in zip(range(len(df1)), range(len(df2))):
exec(f"result{i}=df1.loc[{i},'lat']-df2.loc[{j},'lat']")

But it only gave me one result value for each result, instead of 8 values for each result.但它只给了我每个结果的一个结果值,而不是每个结果的 8 个值。 I will appreciate any possible solution.我将不胜感激任何可能的解决方案。 Thanks!谢谢!

You can create list of Series :您可以创建Series列表:

L = [df1.loc[i,'lat']-df2['lat'] for i in df1.index]

Or you can use numpy for new DataFrame :或者您可以将 numpy 用于新的DataFrame

arr = df1['lat'].to_numpy() - df2['lat'].to_numpy()[:, None]
df3 = pd.DataFrame(arr, index=df2.index, columns=df1.index)
print (df3)
           0          1          2          3          4          5  \
0  -0.829833  -0.479833   1.953500   1.886833   0.620167  -0.429833   
1  -1.124223  -0.774223   1.659110   1.592443   0.325777  -0.724223   
2  -1.086263  -0.736263   1.697070   1.630403   0.363737  -0.686263   
3  -1.110113  -0.760113   1.673220   1.606553   0.339887  -0.710113   
4  -1.330463  -0.980463   1.452870   1.386203   0.119537  -0.930463   
5  -1.389173  -1.039173   1.394160   1.327493   0.060827  -0.989173   
6  -2.162067  -1.812067   0.621266   0.554599  -0.712067  -1.762067   
7  -1.950906  -1.600906   0.832427   0.765760  -0.500906  -1.550906   
8  15.389667  15.739667  18.173000  18.106333  16.839667  15.789667   

           6          7  
0  -0.596500  -1.146500  
1  -0.890890  -1.440890  
2  -0.852930  -1.402930  
3  -0.876780  -1.426780  
4  -1.097130  -1.647130  
5  -1.155840  -1.705840  
6  -1.928734  -2.478734  
7  -1.717573  -2.267573  
8  15.623000  15.073000  

Since df1 has one less row than df2由于 df1 比 df2 少一行

df1['lat'] = df1['lat'] - df2.loc[:df1.shape[0]-1, 'lat']

output: output:

0   -0.829833
1   -0.774223
2    1.697070
3    1.606553
4    0.119537
5   -0.989173
6   -1.928734
7   -2.267573
Name: lat, dtype: float64

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何根据一行是否包含另一行中的值组合数据框中的行 - How to combine rows in dataframe based on if a row contains a value in another row 如果dataframe行为1,其余值为0,如何设置最大值 - How do I set the max value if dataframe row to 1 and the rest of the values to 0 如果数据框中的值与另一行中的值匹配,则更改它 - Change values in a dataframe if it matches a value in another row 当一行中某一列的值与另一行另一列中的值匹配时,如何匹配pyspark数据框中的两行? - How can I match two rows in a pyspark dataframe when the value in a column in a row matches the value in another column in another row? 在 pandas dataframe 中使用另一个 dataframe 的行中的多个值查找值 - lookup value in the pandas dataframe using the muliple values in the row of another dataframe 如何使用另一个数据帧中的行减去数据框中的所有行? - How to subtract all rows in a dataframe with a row from another dataframe? 如何将PANDAS数据帧的一行添加到其余行? - How do I add one row of a PANDAS dataframe to the rest of the rows? 如果另一个数据帧中存在相同的行,如何删除Pandas数据帧中的行? - How to remove rows in a Pandas dataframe if the same row exists in another dataframe? 如果值在另一个数据帧中,如何从一个数据帧获取行 - How to get rows from one dataframe if values are in another dataframe 对于第一个 df 中值的所有实例,如何用另一个 dataframe 的值替换 pandas 行? - How do I replace pandas rows with values of another dataframe for all instances of the value in the first df?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM