Search code examples
pythondataframejupyter-notebooksentiment-analysis

How to make a table or DataFrame from dtype=object?


I would like to show how the predictions are made in a table or DataFrame.

I tried to put X_test, y_test and predictions (predictions = model.predict(X_test)) into a DataFrame to show which reviews are positive or negative predicted.

import pandas as pd

predictions = model.predict(X_test)
df_prediction = pd.DataFrame({
    'X_test': [X_test],
    'y_test': [y_test],
    'prediction': [predictions]
})
df_prediction.head()

Variable "X_test" (Name: review, Length: 4095, dtype: object) looks like:

15806 ['tire', 'gauges', 'kind', 'thing', 'makes', '...

541 ['like', 'said', 'title', 'review', 'say', 'pr...

...

Variable "y_test" (Name: label, Length: 4095, dtype: object) looks like:

15806 positiv

541 positiv

...

Variable "predictions" looks like:

array(['positiv', 'positiv', 'positiv', ..., 'positiv', 'positiv', 'positiv'], dtype=object)

At the moment I got a DataFrame with all Data in the first line but I need as a table with all lines.


Solution

  • If x_test, y_test and predictions are lists, then you could just do this:

    df_prediction = pd.DataFrame({
        'X_test': x_test,
        'y_test': y_test,
        'prediction': predictions
    })
    

    Also, df_prediction.head() will print the first 5 rows of the dataframe. You could use print(df_prediction) or just df_prediction (for formatted output in Jupyter Notebook) to see the entire data.