向行添加值并将其复制到 pandas 中该值的正下方 [英] add values to rows and replicate it just below that value in pandas

查看:43
本文介绍了向行添加值并将其复制到 pandas 中该值的正下方的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在经度上加 150,从纬度中减去 150,然后将它们粘贴到它们的值下方.

I want to add 150 to longitude and subtract 150 from latitude and paste them just below the their value.

OBJECTID     longitude     latitude
0            690187.1250  1870994.875
1            690202.1875  1870956.125
2            690213.0000  1870921.625
3            690223.8125  1871010.000
4            690245.3750  1870971.250
5            690262.6250  1871031.625
6            690286.3125  1870986.250
7            690297.1250  1871053.125
8            690327.3125  1871003.500
9            690331.6250  1871072.625

预期输出:只是一个简短的例子,我想要这个用于所有 txt 文件

Expected output: just short example i want this for all the txt file

Index        longitude     latitude
0            690187.1250  1870994.875
0            690337.1250  1870844.875
1            690202.1875  1870956.125
1            690352.1875  1870806.125

这是我试过的

import pandas as pd
df = pd.read_csv('F:\\pointcsv.txt', delimiter = ",")
def f1(d):
    dn = d.copy()
    for x in range(len(d)-2):
            idx = x + 0.5
            dn.loc[idx] = (d.iloc[x]['latitude'] - 150) and (d.iloc[x]['longitude'] - 150)       
    dn = dn.sort_index().reset_index(drop=False)
    return dn
nd = f1(df)
print(nd)

这仅适用于经度.我如何同时做这件事.有人可以帮我解决这个问题

This is only working for longitude.how do i do it both at same time.Can someone help me fix this

推荐答案

您可以在 ['longitude','纬度'] 和 concat + sort_index

You can add 150 and -150 to each column of ['longitude','latitude'] and concat + sort_index

out = (pd.concat((df[['longitude','latitude']],df[['longitude','latitude']]
         .add([150,-150]))).sort_index())


print(out)


     longitude     latitude
0  690187.1250  1870994.875
0  690337.1250  1870844.875
1  690202.1875  1870956.125
1  690352.1875  1870806.125
2  690213.0000  1870921.625
2  690363.0000  1870771.625
3  690223.8125  1871010.000
3  690373.8125  1870860.000
4  690245.3750  1870971.250
4  690395.3750  1870821.250
5  690262.6250  1871031.625
5  690412.6250  1870881.625
6  690286.3125  1870986.250
6  690436.3125  1870836.250
7  690447.1250  1870903.125
7  690297.1250  1871053.125
8  690477.3125  1870853.500
8  690327.3125  1871003.500
9  690331.6250  1871072.625
9  690481.6250  1870922.625

这篇关于向行添加值并将其复制到 pandas 中该值的正下方的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆