Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get pandas to print out data and not the memory address?

I am currently trying to print data from a text file using pandas,JSON and Python 3.4.

When I run the code on Python 2.7 on my friend's machine it works fine, but not with Python 3.4.

Here is my code:

import json
import pandas as pd

tweets_data_path = 'tweets.txt'

tweets_data = []
tweets_file = open(tweets_data_path, "r")
for line in tweets_file:
    try:
        tweet = json.loads(line)
        tweets_data.append(tweet)
    except:
        continue

print (len(tweets_data))

tweets = pd.DataFrame()
tweets['text'] = map(lambda tweet: tweet['text'], tweets_data)
tweets['lang'] = map(lambda tweet: tweet['lang'], tweets_data)
tweets['country'] = map(lambda tweet: tweet['place']['country'] if tweet['place'] != None else None, tweets_data)

for i in range(len(tweets_data)):
    print(tweets['text'][i])

Instead of printing the tweet data, it prints the memory location of the data. e.g.

<map object at 0x04988050>
<map object at 0x04988050>

How do I get it to print out the actual tweet data?

like image 610
Joseph Avatar asked Nov 01 '22 00:11

Joseph


1 Answers

You need to convert it to a list first. So just change map(lambda...) to list(map(lambda...))

like image 172
fosho Avatar answered Nov 15 '22 05:11

fosho