0

This is likely a very simple question but I would appreciate help!

As part of a larger script, I have a dataframe (imported from a csv file) with two columns, 'file_name' and 'value'. I have a short example below:

            file_name  value
0  201623800811s.fits   True
1  201623802491s.fits   True
2  201623802451s.fits  False

I would like to define a function that reads the values within column 'value', and returns 0 for 'False' and 1 for 'True'. I would then like to append the results to a third column in the dataframe, and finally export the updated dataframe to the csv.

I have defined a function that appears to me to work. However, when I run the script it does not execute and I receive the message:

<function convert_string at 0x000000000DE35588>

In the console. My function is below. Any help or advice will be welcomed.

def convert_string(explosions):
    for i in range(0,len(explosions)):
        if i == 'True' :
            return 1
        elif i == 'False' :
            return 0
        else:
            return 2

print convert_string 
1
  • You never call your function. Commented Jun 29, 2017 at 12:09

2 Answers 2

1

If you are using an explicit for loop when working with a dataframe, you are most probably "doing it wrong". Also, what is the point of having a for loop if you return on the very first iteration?

Consider these:

import numpy as np

df['third_column'] = np.where(df['value'], 1, 0)

If you insist on defining a function:

def foo(x):
    return int(x)

df['third_column'] = df['value'].apply(foo)

or simply

df['third_column'] = df['value'].apply(lambda x: int(x))

Full example:

import pandas as pd
import numpy as np

df = pd.DataFrame({'value': [True, False]})
print(df)

#     value
#  0   True
#  1  False

df['third_column'] = np.where(df['value'], 1, 0) 
print(df)

#     value  third_column
#  0   True             1
#  1  False             0
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, this is a useful set of alternatives written much more elegantly than my own code. When you say that explicit use of a for loop with a dataframe is "doing it wrong", is that because your dataframe already defines the data you wish to use?
0

You're not calling the function. Your print statement should be: print convert_string(<value>), where <value> is an integer.

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.