5

I think I have a relatively simply question but am not able to locate an appropriate answer to solve the coding problem.

I have a pandas column of string: df1['tweet'].head(1) 0 besides food, Name: tweet

I need to extract the text and push it into a Python str object, of this format:

test_messages = ["line1", "line2", "etc"]

The goal is to classify a test set of tweets and therefore believe the input to: X_test = tfidf.transform(test_messages) is a str object.

3
  • Are you just trying to do list(df1["tweet"])... ? Commented Jun 15, 2013 at 15:57
  • @AndyHayden looks like you saved the day again... thanks. Commented Jun 15, 2013 at 16:12
  • Related: Pandas - Get first row value of a given column Commented Sep 3, 2018 at 6:00

3 Answers 3

3

Use list convert a Series (column) into a python list:

list(df1["tweet"])
Sign up to request clarification or add additional context in comments.

Comments

1
  1. Get the Series head(), then access the first value:

    df1['tweet'].head(1).item()

  2. or: Use the Series tolist() method, then slice the 0'th element:

    df.height.tolist() [94, 170] df.height.tolist()[0] 94

(Note that Python indexing is 0-based, but head() is 1-based)

Comments

1

Option 1 : df1['tweet'][0] or df1.loc[0, 'tweet']
Option 2 : df1['tweet'].to_list()[0]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.