user5779223
user5779223

Reputation: 1490

How to call the index value of the data frame as an arg in apply function with python-pandas?

I have a data frame df with following format:

        text
0       hi john
1       how are you
2       666
3       so crazy
...

And I wish to write each row one by one in a different file with the index number as the file name. For instance, 'hi john' should be written to 0.txt

Given the function:

def writeText(text, index):
    with open(str(number)+'.txt', 'w+') as f:
        f.write(text)

Now I need to apply it to df:

df.apply(writeText, args=(??????, ))

What args should I pass to the function so that the index number can be used? Thanks in advance!

Upvotes: 1

Views: 209

Answers (1)

jezrael
jezrael

Reputation: 862731

I think you can first convert text column to values and then use numpy.ndarray.tofile in loop for writing data:

print df

          text
0      hi john
1  how are you
2          666
3     so crazy

for row in df.text.reset_index().values:
    print row
    row[1:].tofile(str(row[0])+'.txt', sep="\t", format="%s")

    [0L 'hi john']
    [1L 'how are you']
    [2L '666']
    [3L 'so crazy']

Another option:

def writeText(df):
    for row in df.text.reset_index().values:
        with open(str(row[0])+'.txt', 'w+') as f:
            f.write(str(row[1]))

writeText(df)

But if you need apply function to DataFrame use iteritems:

def writeText(x):
    for row in x.iteritems():
        with open(str(row[0])+'.txt', 'w+') as f:
            f.write(str(row[1]))

df.apply(writeText)

Upvotes: 1

Related Questions