2

I am currently using this code:-

print(df.to_csv(r'tweets.txt', header=None, index=None, sep=' ', mode='a'))

By this I am getting all the columns in the text file, but I only want a single column.Does anyone know how can I do it? Thanks in advance

1 Answer 1

1

just sub-select the col of interest:

print(df[col_of_interest].to_csv(r'tweets.txt', header=None, index=None, sep=' ', mode='a'))

by doing df[col_of_interest] you only select that single column which will return a Series, if you want a dataframe use double square brackets:

print(df[[col_of_interest]].to_csv(r'tweets.txt', header=None, index=None, sep=' ', mode='a'))

You can also pass a single list item to param columns:

print(df.to_csv(r'tweets.txt', header=None, index=None, sep=' ', mode='a', columns=['col_of_interest']))

see the docs

Sign up to request clarification or add additional context in comments.

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.