gpt4 book ai didi

python - 如何比较 3 对 float 类型的 pandas dataframe 列?

转载 作者:行者123 更新时间:2023-11-30 22:43:30 24 4
gpt4 key购买 nike

我想比较一个数据帧的 3 个不同对。在我的数据框的列中,某些值不是浮点类型,即“,”位于这些值之间,因此我想从该值中删除“,”,然后我想将该列转换为浮点类型。最后一步是比较三对不同的数据框列。

数据框:

         aaa float_type1 float_type2 float_type3 float_type4 float_type5 float_type6
0 abc 1.12 1.120 1.20 1.2 1,67 167
1 xyz 1,2.5 2.35 1.25 125 12,5 12.5
2 pqr 3.56 3.58 35.6 3.78 3.90 5.56
3 pqr 5.5 5.8 5.05 5.005 5.500 5,5.78
4 pqr 6.6 6.9 6.06 6.06 6.60 6.600

程序:

def float_type_format(arg):

arg = arg.replace(',', '')
return arg

data = {'aaa' :{0:'abc',1:'xyz',2:'pqr',3:'pqr',4:'pqr'},
'float_type1' :{0:'1.12',1:'1,2.5',2:'3.56',3:'5.5',4:'6.6'},
'float_type2' :{0:'1.120',1:'2.35',2:'3.58',3:'5.8',4:'6.9'},
'float_type3' :{0:'1.20',1:'1.25',2:'35.6',3:'5.05',4:'6.06'},
'float_type4' :{0:'1.2',1:'125',2:'3.78',3:'5.005',4:'6.06'},
'float_type5' :{0:'1,67',1:'12,5',2:'3.90',3:'5.500',4:'6.60'},
'float_type6' :{0:'167',1:'12.5',2:'5.56',3:'5,5.78',4:'6.600'}}

df1 = pd.DataFrame(data)

#removing "," from float values
df1['float_type1'] = df1['float_type1'].apply(float_type_format)
df1['float_type2'] = df1['float_type2'].apply(float_type_format)
df1['float_type3'] = df1['float_type3'].apply(float_type_format)
df1['float_type4'] = df1['float_type4'].apply(float_type_format)
df1['float_type5'] = df1['float_type5'].apply(float_type_format)
df1['float_type6'] = df1['float_type6'].apply(float_type_format)


#converting dtype into float
df1.float_type1 = df1.float_type1.astype('float')
df1.float_type2 = df1.float_type2.astype('float')
df1.float_type3 = df1.float_type3.astype('float')
df1.float_type4 = df1.float_type4.astype('float')
df1.float_type5 = df1.float_type5.astype('float')
df1.float_type6 = df1.float_type6.astype('float')

为了从列值中删除“,”,我遵循上述逻辑。

问题 1:

他们有快速的性能和从列中删除“,”的好方法吗?

现在我想比较 float_type1 与 float_type2、float_type3 与 float_type4、float_type5 与 float_type6,如果所有 3 对都相等,则只有结果列包含 true 且预期输出如下:

aaa  float_type1  float_type2  float_type3  float_type4  float_type5  \
0 abc 1.12 1.12 1.2 1.2 167.0

float_type6 result
0 167.0 True

问题2:

我想要一种可靠的方法来执行此比较。

最佳答案

通过将 replaceregex=True 结合使用,您可以大大简化代码。然后,您可以使用 pd.to_numeric 转换为数字,然后您可以使用一些 bool 逻辑获取结果列。

df2 = df1.replace(',','',regex=True)
df2 = df2.apply(pd.to_numeric, errors='ignore')

df2['result'] = ((df2['float_type1'] == df2['float_type2']) &
(df2['float_type3'] == df2['float_type4']) &
(df2['float_type5'] == df2['float_type6']))

aaa float_type1 float_type2 float_type3 float_type4 float_type5 \
0 abc 1.12 1.12 1.20 1.200 167.0
1 xyz 12.50 2.35 1.25 125.000 125.0
2 pqr 3.56 3.58 35.60 3.780 3.9
3 pqr 5.50 5.80 5.05 5.005 5.5
4 pqr 6.60 6.90 6.06 6.060 6.6

float_type6 result
0 167.00 True
1 12.50 False
2 5.56 False
3 55.78 False
4 6.60 False

关于python - 如何比较 3 对 float 类型的 pandas dataframe 列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41742015/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com