[英]Could not convert string to float Python - Pandas DataFrames
Hi I am getting this error but everything seems ok.嗨,我收到此错误,但一切似乎都正常。
import matplotlib.pyplot as mpl
import pandas as pd
#Uploading data to Python Pandas Dataframe
db_fondos = pd.read_excel('/Users/jonathanprieto/Documents/GitHub/GoogleMaps/data/- Matriz TDF Python.xlsm',
sheet_name="DB Fondos")
ts_flujos = pd.read_excel('/Users/jonathanprieto/Documents/GitHub/GoogleMaps/data/- Matriz TDF Python.xlsm',
sheet_name="TS Flujos")
ts_ind_fin = pd.read_excel('/Users/jonathanprieto/Documents/GitHub/GoogleMaps/data/- Matriz TDF Python.xlsm',
sheet_name="TS Ind. Fin.")
ts_market = pd.read_excel('/Users/jonathanprieto/Documents/GitHub/GoogleMaps/data/- Matriz TDF Python.xlsm',
sheet_name="TS Market data")
db_posiciones = pd.read_excel('/Users/jonathanprieto/Documents/GitHub/GoogleMaps/data/- Matriz TDF Python.xlsm',
sheet_name="DB Posiciones")
print('Carga completada')
#NaN = np.nan
#db_posiciones["Id. Pos."] = NaN
#db_posiciones.head()
print(db_posiciones)
diccionario=db_fondos.set_index("Fondo")["Id. Fondo"].to_dict()
for index, row in db_posiciones.iterrows():
ipos = row["Fondo"]
print(ipos)
if ipos in diccionario:
idpos=diccionario[ipos]
twofirst=row["Fondo"][:2]
twofirst = twofirst[0:2]
print(idpos+"-"+twofirst)
db_posiciones["Id. Pos."].values[index] = str(idpos)+"-"+str(twofirst)
print(index)
print(db_posiciones)
Could anyone told me why I am getting this error:谁能告诉我为什么会收到此错误:
File "/Users/jonathanprieto/Documents/GitHub/GoogleMaps/test.py", line 36, in <module>
db_posiciones["Id. Pos."].values[index] = str(idpos) + "-" + str(twofirst)
ValueError: could not convert string to float: '91_AGSACB_08-65' ValueError:无法将字符串转换为浮点数:'91_AGSACB_08-65'
You're getting this because your series is string, but it contains some NAs, which actually get represented in pandas as nan
, which is a float value (that's how pd.read_csv()
will handle it).你得到这个是因为你的系列是字符串,但它包含一些 NA,实际上在 pandas 中表示为
nan
,这是一个浮点值(这就是pd.read_csv()
将如何处理它)。 That's why pandas gives a strange warning claiming the string series is a float:这就是为什么 pandas 给出一个奇怪的警告,声称字符串系列是一个浮点数:
df[column].fillna('', inplace=True)
Notes:笔记:
fillna(..., inplace=True)
so you don't need to assign the result back to df['column']
to prevent it getting thrown away.fillna(..., inplace=True)
这样您就不需要将结果分配回df['column']
以防止它被丢弃。pd.Series.fillna(..., inplace=True)
pd.Series.fillna(..., inplace=True)
df.fillna
rather than df[column].fillna(..., inplace=True)
on each column.df.fillna
而不是df[column].fillna(..., inplace=True)
。 See pd.DataFrame.fillna()
pd.DataFrame.fillna()
df[column] = df[column].apply(lambda x: float(x.split()[0].replace(',', '')))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.