3

I'm trying to fill in value for a specific cell in a dataframe that already has some info in it. When I test it with small data it works but in my project it throws "ValueError : cannot convert string to float"

# df : a dataframe with data to be extracted and added to another dataframe data

some_list = df.iloc[:, 0].values
values = df.iloc[:, 1].values

indexList = data.index.values.tolist()

index = 0
for x in some_list:
    s = values[index]
    if x in indexList:
        # this line raised ValueError : cannot convert __(values in s)__ from string to float
        data.at[x, 'Some Column'] = s
    index += 1

elements in some_list and values are strings. Here the existing dataframe is data, where one of the column is 'Some Column', and index labels are some elements in some_list (therefore if the current element x in some_list is one of the index label, set value at the cell (row : index label x, column : 'Some Column' ) to values[the same row that this x is found]

before filling in cells under 'Some Column' should be NaN

A small sample I tried testing the .at function with (that didn't raised valueError):

column=['Year', 'first', 'second', 'third']
s = 'label1'
data = pd.DataFrame(columns=column)
row = pd.Series({'Year' : 2019, 'first' : 'asa', 'second' : 'awdqw'}, name=s)
data = data.append(row)


# Creates a pandas DataFrame. 
data_snip = {'name':['A', 'B', 'C', 'D'], 'y':[1, 2, 3, 4]} 
df = pd.DataFrame(data_snip, index =['one', 'two', 'three', 'four'])
df_array = df.iloc[:, 0].values
s = df_array[3] # should be 'D'

s = 'label3'
row = pd.Series({'Year' : 2030, 'first' : 'ubeubf', 'second' : 'qov.z'}, name=s)
data = data.append(row)
data.at['label3', 'third'] = s

and this is what data looks like :

        Year   first second third
label1  2019     asa  awdqw   NaN
label3  2030  ubeubf  qov.z     D

Any help would be greatly appreciated !!

1 Answer 1

4

Check df.dtypes - your error is coming from the fact, that you are trying to pack string object into column with dtype float. .at[] doesn't do any auto casting of column type.

Before assigning string to a cell, make sure it's of object dtype:

import pandas as pd

import numpy as np

df=pd.DataFrame(data={"x": list("abc"), "y": [1,2,3], "z": [np.nan for i in range(3)]}, index=["p", "q", "r"])

print(df)
#this works fine:
df.at["r", "z"]=4.5

print(df)
#without the line below it will fail:
df["z"]=df["z"].astype(str)
df.at["q", "z"]="z"
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.