如何在pandas中将字符串转换为浮点数

4

我正在尝试将数据集中的字符串转换为浮点数类型。以下是一些背景信息:

import pandas as pd
import numpy as np
import xlrd
file_location = "/Users/sekr2/Desktop/Jari/Leistungen/leistungen2_2017.xlsx"
workbook = xlrd.open_workbook(file_location)
sheet = workbook.sheet_by_index(0)

df = pd.read_excel("/Users/.../bla.xlsx")

df.head()

    Leistungserbringer Anzahl Leistung     AL      TL      TaxW    Taxpunkte
 0  McGregor Sarah  12  'Konsilium'     147.28  87.47   KVG     234.75
 1  McGregor Sarah  12  'Grundberatung' 47.00   67.47   KVG     114.47
 2  McGregor Sarah  12  'Extra 5min'    87.28   87.47   KVG     174.75
 3  McGregor Sarah  12  'Respirator'    147.28  102.01  KVG     249.29
 4  McGregor Sarah  12  'Besuch'        167.28  87.45   KVG     254.73

为了继续进行,我需要找到一种创建新列的方法: df['Leistungswert'] = df['Taxpunkte'] * df['Anzahl'] * df['TaxW'].
每个条目中的TaxW显示字符串'KVG'。 根据数据,我知道'KVG'= 0.89。 我试图将该字符串转换为浮点数,但却遇到了困境。 因为这段代码将用于进一步输入,所以不能只创建一个新列并指定浮点类型。在列TaxW中,大约有7个不同的条目,其值都不同。
感谢提供有关此问题的所有信息。 KVG = 0.92

你能展示一下其他数据,其中表明'KVG' = 0.89吗? - IanS
我添加了一张截图。 - Jari Klingler
2个回答

5
假设'KVG'不是TaxW中唯一可能的字符串值,您应该存储一个将字符串映射到其浮点等效值的映射,如下所示:
map_ = {'KVG' : 0.89, ... } # add more fields here 

然后,您可以使用 Series.map

In [424]: df['Leistungswert'] = df['Taxpunkte'] * df['Anzahl'] * df['TaxW'].map(map_); df['Leistungswert']
Out[424]: 
0    2507.1300
1    1222.5396
2    1866.3300
3    2662.4172
4    2720.5164
Name: Leistungswert, dtype: float64

或者,您可以使用df.transform

In [435]: df['Leistungswert'] = df.transform(lambda x: x['Taxpunkte'] * x['Anzahl'] * map_[x['TaxW']], axis=1); df['Lei
     ...: stungswert']
Out[435]: 
0    2507.1300
1    1222.5396
2    1866.3300
3    2662.4172
4    2720.5164
Name: Leistungswert, dtype: float64

我不确定它会像那样工作。但是我没有注意到自从Pandas 0.20.0以来我们有这样一个新的函数 - 这对我来说是一个很好的发现。 - MaxU - stand with Ukraine
@MaxU 啊,抱歉。应该放一个可工作的例子。它是有效的。需要指定 axis=1。显然,它是矢量化的,因此它是一个很好的 apply 替代品。 - cs95
1
@cᴏʟᴅsᴘᴇᴇᴅ 我在发布之前尝试过映射,但是我有一个不起作用的版本: mapping = {'KVG' : 0.89, ...} df.applymap(lambda s: mapping.get(s) if s in mapping else s)你的代码完美地解决了问题,谢谢! - Jari Klingler

5

以下是使用 @COLDSPEED 的 map_ 映射的替代方案:

In [237]: df.assign(TaxW=df['TaxW'].map(map_)) \
            .eval("Leistungswert = Taxpunkte * Anzahl * TaxW", inplace=False)
Out[237]:
  Leistungserbringer  Anzahl       Leistung      AL      TL  TaxW  Taxpunkte  Leistungswert
0     McGregor Sarah      12      Konsilium  147.28   87.47  0.89     234.75      2507.1300
1     McGregor Sarah      12  Grundberatung   47.00   67.47  0.89     114.47      1222.5396
2     McGregor Sarah      12     Extra 5min   87.28   87.47  0.89     174.75      1866.3300
3     McGregor Sarah      12     Respirator  147.28  102.01  0.89     249.29      2662.4172
4     McGregor Sarah      12         Besuch  167.28   87.45  0.89     254.73      2720.5164

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接