[英]How to convert str to float in pandas
I'm trying to convert a string of my dataset to a float type. 我正在尝试将我的数据集字符串转换为浮点类型。 Here some context:
这里有一些背景:
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
To keep working on this I need to find a way to create a new column: df['Leistungswert'] = df['Taxpunkte'] * df['Anzahl'] * df['TaxW']
. 为了继续努力,我需要找到一种方法来创建一个新列:
df['Leistungswert'] = df['Taxpunkte'] * df['Anzahl'] * df['TaxW']
。
TaxW shows the string 'KVG' for each entry. TaxW显示每个条目的字符串'KVG'。 I know from the data that 'KVG' = 0.89.
我从数据中得知'KVG'= 0.89。 I have hit a wall with trying to convert the string into a float.
我试图将字符串转换为浮点数而撞墙。 I cannot just create a new column with the float type because this code should work with further inputs.
我不能只使用float类型创建一个新列,因为此代码应该与其他输入一起使用。 In the column TaxW there are about 7 different entries with all different values.
在TaxW列中,大约有7个不同的条目具有所有不同的值。
I'm thankful for all information on this matter. 我很感谢有关此事的所有信息。
Assuming 'KVG'
isn't the only possible string value in TaxW
, you should store a mapping of strings to their float equivalent, like this: 假设
'KVG'
不唯一可能的字符串值TaxW
,应该字符串的映射存储到其浮等同,就像这样:
map_ = {'KVG' : 0.89, ... } # add more fields here
Then, you can use Series.map
: 然后,您可以使用
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
Alternatively, you can use df.transform
: 或者,您可以使用
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
Alternative solution which uses map_
mapping from @COLDSPEED: 使用
map_
mapping的替代解决方案:
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.