Home > database >  How to convert individual list values inside a pandas DataFrame into string/float?
How to convert individual list values inside a pandas DataFrame into string/float?

Time:01-18

I have fetched stock data from a website and made a pandas dataframe but there are some error like this photo below...enter image description here

How can I solve this problem?...so that individual cell work like a float/ string instead of list?

CodePudding user response:

Let's say your dataframe is called df. You can simply run:

df.applymap(lambda x: x[0])

CodePudding user response:

Probably the problem stems from the way you convert the incoming data to a dataframe. You can still write a filter function like the following code and apply it to your columns.

>>> df = pd.DataFrame({'column_1': ['[0]', '[1]', '[2]']})

>>> get_digits = lambda x:int("".join(filter(str.isdigit, x)))

>>> df['column_1'].apply(func)

0    0
1    1
2    2
Name: column_1, dtype: int64

CodePudding user response:

If your data are strings, use a regex to remove brackets:

df = df.replace(r'[\[\]]', '', regex=True)
  •  Tags:  
  • Related