我尝试创建一个以点为坐标的列作为一个numpy数组。我有一个数据,如东和北。我想简单地通过将其向下移动来减少大量数据。我尝试用Unittest进行测试
我尝试遵循其他问题,.apply(lambda)
但可以解决我的问题。(我在pandas 0.9中工作,无法更新)。下面是一个示例代码,而我苦苦挣扎的功能是adjustCoordSystem()
import unittest
import pandas as pd
from pandas.util.testing import assert_frame_equal
def exampleDf():
df = pd.DataFrame({'Easting':{0:11,1:12,2:13,3:14},
'Northing':{0:5,1:7,2:9,3:11}})
return df
def exampWithCoord():
df = exampleDf()
df['Sample']=[[0,0,0],[1,2,0],[2,4,0],[3,6,0]]
return df
class dfProccesedFull():
def adjustCoordSystem(self, df):
''' change coordinate system to get from 0 to max'''
df['Sample'] = \
[df['Easting'].apply(lambda x: x - min(df['Easting'])),
df['Northing'].apply(lambda x: x - min(df['Northing'])),
df['Northing'].apply(lambda x: 0.0)]
# [(df['Easting'] - min(df['Easting'])), (df['Northing'] - min(df['Northing'])),\
# df['Northing'].apply(lambda x: 0.0)]
return df
class TestDfProccesedDataFull(unittest.TestCase):
def test_adjustCoordSystem(self):
df = exampleDf()
dfModel = exampWithCoord()
tData = dfProccesedFull()
dfTested=tData.adjustCoordSystem(df)
assert_frame_equal(dfTested, dfModel)
if __name__ == "__main__"
unittest.main()
我有一个错误:AssertionError
行:df['Northing'].apply(lambda x: 0.0)]
如何更改函数以在“示例”列中具有数组列表,但不遍历每一行?
我正在寻找的输出是新的数据框,例如:
Easting Northing Sample
0 11 5 [0, 0, 0]
1 12 7 [1, 2, 0]
2 13 9 [2, 4, 0]
3 14 11 [3, 6, 0]
其中“样本”列为[从Easting开始的x坐标,从Northing开始的y坐标,z坐标= 0]
我不确定这是什么意思...您正在尝试将其分配给单个列,除非df的长度为三,否则它将失败:
df['Sample'] = [df['Easting'].apply(lambda x: x - min(df['Easting'])),
df['Northing'].apply(lambda x: x - min(df['Northing'])),
df['Northing'].apply(lambda x: 0.0)]
参见例如:
In [21]: df = pd.DataFrame([[1, 2], [3, 4]], columns=['A', 'B'])
In [22]: df['C'] = [df.copy(), df.copy()] # use copy to avoid max recursion error...
In [23]: df['C'] = [1, 2, 3]
ValueError: Length of values does not match length of index
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句