Python Forum
Convert dataframe from str back to datafarme
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Convert dataframe from str back to datafarme
#1
Hi,

Am looking for help where I converted a dataframe in order for replace function to work?
Now am looking for way to convert it back to datafame ( post the replace) for further processing.

df = str(df)
    df = df.replace('old_value','new_value')
Without converting it to str .replace doesn't work on my dataframe.
Reply
#2
(Jul-07-2023, 01:05 PM)Creepy Wrote: Am looking for help where I converted a dataframe in order for replace function to work?
No,should not need to do that Pandas has of course a replace method.
Post a sample of the DataFrame and what you want to replace.
Example.
import pandas as pd
from io import StringIO

data = StringIO('''\
CarTypes Specialty
BMV H
Volvo-kkk I
Honda J
Kia-789 K
Fiat-55 L''')

df = pd.read_csv(data, sep=' ')
Test.
>>> df
    CarTypes Specialty
0        BMV         H
1  Volvo-kkk         I
2      Honda         J
3    Kia-789         K
4    Fiat-55         L

>>> df['CarTypes'] = df['CarTypes'].replace(['Volvo-kkk'], 'Volvo')
>>> df
  CarTypes Specialty
0      BMV         H
1    Volvo         I
2    Honda         J
3  Kia-789         K
4  Fiat-55         L

>>> # Using regex
>>> df = df.replace(to_replace ='-\d.*', value='', regex=True)
>>> df
  CarTypes Specialty
0      BMV         H
1    Volvo         I
2    Honda         J
3      Kia         K
4     Fiat         L
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Convert python dataframe to nested json kat417 1 6,354 Mar-18-2022, 09:14 PM
Last Post: kat417
  Cannot convert the series to <class 'int'> when trying to create new dataframe column Mark17 3 8,551 Jan-20-2022, 05:15 PM
Last Post: deanhystad
  Convert MultiLayer XML to DataFrame using Pandas vsingh17 0 2,059 Apr-14-2021, 03:50 PM
Last Post: vsingh17
  convert list to five columns dataframe in sequence tonycat 2 2,493 Sep-29-2020, 06:47 AM
Last Post: tonycat
  How do I convert this string back to a list of integers? donmerch 6 3,739 Apr-05-2020, 06:43 PM
Last Post: donmerch
  convert a character to numeric and back Skaperen 2 2,116 Jan-28-2020, 09:32 PM
Last Post: Skaperen
  How to remove html content from a column of the datafarme in Python3.6? PrateekG 3 3,171 Jul-26-2018, 08:18 AM
Last Post: Larz60+
  How to convert c_void_p PyObject back to void* lfdm 0 3,912 Feb-02-2017, 09:13 AM
Last Post: lfdm

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020