Problem in elemintaing the brackets () and post processing the dataframe using Pandas in Python

Viewed 67

I am just a beginner in the Python so kindly excuse for this question, I tried a lot to get it done, but failed, thus I am posting this. I have a data set which looks like:

  5.96303e-07             (11.6667 3.21427 -2.20471e-07)             (11.8746 -1.75419 -2.37923e-07)             (8.66991 -2.84873 5.29442e-07)             (2.19427 13.547 1.16203e-05)
  9.67139e-07             (11.6171 3.16081 -8.83286e-08)             (11.8851 -1.763 -4.38136e-07)             (8.68988 -2.85339 1.81039e-07)             (1.61058 13.629 4.42662e-07)
  1.34613e-06             (11.5562 3.11037 -7.74061e-08)             (11.8897 -1.77006 -3.81523e-07)             (8.70652 -2.8608 8.00436e-08)             (1.47268 13.5569 -2.03173e-06)
  1.73261e-06             (11.4961 3.06921 -1.49294e-07)             (11.8919 -1.77567 -3.48887e-07)             (8.71974 -2.86802 5.2652e-08)             (1.59798 13.4556 -2.52073e-06)
  2.12563e-06             (11.4423 3.03706 -1.53771e-07)             (11.8932 -1.78022 -3.33928e-07)             (8.73 -2.87398 4.65075e-08)             (1.77817 13.3679 -2.42045e-06)

Now when I am accessing the data frame for an instance df.iloc[:,1] it gives me (11.6171, when I tried to plot it --it gives me error, then I thought that since the "(" is creating a problem I removed that using df.replace('\(','',regex=True).replace('\)','',regex=True) . The plot function seems to work but gives very weird figure(not allowed to post the figure). In addition to that when I tried to do some calculations like (df.iloc[:,1])^2 it is giving me errors which says:

TypeError: can't multiply sequence by non-int of type 'str' 

I guess the data is not in the correct form. Any comment or suggestion will be a great help. Thanks in advance.

2 Answers

There are two relatively minor issues. Something like the following might be what you're looking for. Maybe.

First, the column you are trying to plot is a string. Essentially it contains letters/symbols. Even when you remove the "(" ")" the "numbers" are still considered a string.

# To convert a "3.14" (string) to a 3.14 (float) 
# floats are basically decimals
my_string = "3.14"
my_number = float(my_string)

Additionally, there are multiple "numbers" in the string. So to plot the numbers in that column, I think you would first need to split the string and then convert to numbers.

# Use your code to replace the special characters
df.replace('\(','',regex=True).replace('\)','',regex=True)

# new data frame with split value columns 
new = df["colname_with_three_numbers"].str.split(" ", n = 2, expand = True) 

# Making separate first name column from new data frame 
df["first_number"]= new[0]  
df["second_number"]= new[1] 
df["third_number"]= new[2]

# change the type to allow you to plot something like this should work
df["first_number"] = float(df["first_number"])

df

This is a pretty bad method to solve this, but if the dataset is not too large you can get each element using a for loop and remove the brackets using str.replace(")","").

Related