Alex
Alex

Reputation: 1537

Iterate over pandas dataframe rows as pure text

I would like to read a dataframe (which contains tweets) row by row in order to analyze the text.

import csv
import pandas as pd

df = pd.read_csv('tweets2.csv')
df.head()

for row in df.iterrows():
    print (row)

This code I wrote does not do the job, since the "row" also includes the index. Instead, I want the simple text, that I will process further.

Upvotes: 1

Views: 1952

Answers (2)

user2124834
user2124834

Reputation:

iterrows yields (index, Series) pairs

So you could unpack them in the for loop:

for i, row in df.iterrows():
    print(row)

If you don't use the i, you should change it to _.

Using iterrows, each row is a Series. As shown by @cᴏʟᴅsᴘᴇᴇᴅ, an alternative is using values:

for row in df.values:
    print(row)

With this method each row is a numpy array (so labeling is lost).

Upvotes: 0

cs95
cs95

Reputation: 402463

You could use df.values:

for row in df.values:
    print(row)

Example:

df = pd.DataFrame({'Col1': [1, 2, 3, 4, 5], 'Col2' : ['a', 'b', 'c', 'd', 'e']})

print(df)

   Col1 Col2
0     1    a
1     2    b
2     3    c
3     4    d
4     5    e

for row in df.values:
    print(row)

[1 'a']
[2 'b']
[3 'c']
[4 'd']
[5 'e']

Upvotes: 2

Related Questions