简体   繁体   中英

When converting a pandas dataframe to csv, how can i seperate the headers of the dataframe into different columns of the csv-file?

I am trying to scrape data from twitter by using the snscrape. After getting a list with 100 tweets, i am creating a dataframe with the tweets list by using pandas. After converting the dataframe to a csv it looks like the following:

https://i.stack.imgur.com/YRz0S.png

I would like to seperate the headers of the dataframe, so that one header of the dataframe defines one column in the csv-file. How can i do that?

Here you can see my current code:

import snscrape.modules.twitter as sntwitter
import pandas

# Creating list to append tweet data to
tweets_list2 = []

# Using TwitterSearchScraper to scrape data and append tweets to list
for i, tweet in enumerate(
        sntwitter.TwitterSearchScraper('').get_items()):
    if i > 100:
        break
    tweets_list2.append([tweet.date, tweet.id, tweet.content, tweet.user.username])

# Creating a dataframe from the tweets list above
tweets_df2 = pandas.DataFrame(tweets_list2, columns=['Datetime', 'Tweet Id', 'Text', 'Username'])

# converting the json string to a csv
export_csv = tweets_df2.to_csv (r'C:\Users\...\Desktop\Twitter2.csv', index = False)

By the way, I am a bloody beginner with python. Thank you in advance!

try this

import pandas as pd
export_csv = pd.DataFrame({'Datetime,Tweet id,Text,Username': ['dfdnkdf,dfadfdsa,dfddfdfd,sdfdkjdfjjjjfd']})
export_csv = export_csv. iloc[:, 0].str.split(',', expand=True)
export_csv.columns =['Datetime', 'Tweet id', 'Text', 'Username']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM