使用其他两列中的数组创建新列,并在python pandas中对其进行测试

tomasz74

我尝试创建一个以点为坐标的列作为一个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]

安迪·海登(Andy Hayden)

我不确定这是什么意思...您正在尝试将其分配给单个列,除非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] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Python:根据其他两列中的值有条件地创建新列

如何根据其他两列中的 IF AND 使用设定值创建新列

Pyspark-使用数据框中其他两个列的RMSE创建新列

如何通过使用python中的现有列创建以其他列为条件的新列

Pandas:创建一个新列,在其他两列中的值之间交替

比较df的两列并在值增加的情况下创建新列,然后在其他列中写增加,对于减少则写相同

熊猫使用其他列中的值创建新列,并根据列值进行选择

创建头文件并在C中对其进行测试

从Pandas数据框中的其他列创建新列

根据其他列在Pandas DataFrame中创建新列

如何使用dplyr以R中其他两个列的出现为条件创建一个新列?

根据其他两列中的条件在R中创建一个新列

根据其他列中的值在python 3(pandas)数据框中创建新列

如何基于Python Pandas中的其他列在DataFrame中创建新列?

Python Pandas:基于组内的最大值创建新列,但使用其他(字符串)列中的值

如何根据其他两列的值在 DataFrame 中创建新列

根据熊猫中其他两个列的匹配值创建新列

如何根据其他两列中的条件创建和填充新列?

根据其他两个数据框列中的值条件创建新列

根据其他列上的值对列进行分组以在 Pandas 中创建新列

使用Pandas DataFrame中其他两列的键和值创建字典列

Python Pandas:从value不为null的其他列中创建新列

根据python pandas中其他列的值创建新列

Python pandas - groupby之后,如何根据其他列中的值创建新列

从 python pandas 中的其他列创建列

根据其他两列中的值创建是/否列

在 R 中,如何根据其他列的值从其他列的列标题创建新列

通过引用Python中数据框中的其他列来创建新列

根据其他列中的值创建新列