Luqman Buang
Luqman Buang

Reputation: 45

How do I remove square brackets from my dataframe?

Im trying to create a comparison between my predicted and actual values.

Edit: this is the initial dataframe Here is my try:

from sklearn import linear_model
    
reg = linear_model.LinearRegression()
reg.fit(df[['Op1', 'Op2', 'S2', 'S3', 'S4', 'S7', 'S8', 'S9', 'S11', 'S12','S13', 'S14', 'S15', 'S17', 'S20', 'S21']], df.unit)

predicted = []
actual = []
for i in range(1,len(df.unit.unique())):
    xp = df[(df.unit == i) & (df.cycles == len(df[df.unit == i].cycles))]
    xa = xp.cycles.values
    xp = xp.values[0,2:].reshape(1,-2)
    predicted.append(reg.predict(xp))
    actual.append(xa)

and to display the dataframe:

data = {'Actual cycles': actual, 'Predicted cycles': predicted }
df_2 = pd.DataFrame(data)


df_2.head()

I will get an output:

Actual cycles   Predicted cycles
0   [192]   [56.7530579842869]
1   [287]   [50.76877712361329]
2   [179]   [42.72575900074571]
3   [189]   [42.876506912637524]
4   [269]   [47.40087182743173]

ignoring the values that are way off, how do I remove the square brackets in the dataframe? and is there a neater way to write my code? Thank you!

Upvotes: 1

Views: 619

Answers (2)

wwnde
wwnde

Reputation: 26686

print(df_2)

  Actualcycles       Predictedcycles
0        [192]    [56.7530579842869]
1        [287]   [50.76877712361329]
2        [179]   [42.72575900074571]
3        [189]  [42.876506912637524]
4        [269]   [47.40087182743173]

df=df_2.apply(lambda x:x.str.strip('[]'))
print(df)
 Actualcycles     Predictedcycles
0          192    56.7530579842869
1          287   50.76877712361329
2          179   42.72575900074571
3          189  42.876506912637524
4          269   47.40087182743173

Upvotes: 1

jkortner
jkortner

Reputation: 631

Below is a minimal example of your cycles column with brackets:

import pandas as pd

df = pd.DataFrame({
    'cycles' : [[192], [287], [179], [189], [269]]
})

This code gets you the column without brackets:

df['cycles'] = df['cycles'].str[0]

The output looks like this:

print(df)

   cycles
0     192
1     287
2     179
3     189
4     269

Upvotes: 0

Related Questions