How do I convert a string into code in Python?

Jake Stokes

Converting a string to code

Noteworthy points:

  • I'm new to coding and am testing various things to learn;
  • i.e. yes, I'm sure there are better ways do achieve what I am trying to do;
  • I would like to know any alternative / more efficient methods, however;
  • I would also still like to know how to convert string to code to achieve my goal with this technique

So far I have looked around the forum and on google, and seen a few topics on this, none of which I can made work here, or which precisely answer the question from my perspective, including using eval and exec.

The Scenario

  • I have a dataframe: london with 23 columns
  • I want to create a dataframe showing all rows with 'NaN' values
  • I have tried to use .isnull(), but it appears to only work on a single column at a time
  • I am trying to achieve my desired result by using | to return any rows in any columns where .isnull() returns True

An example of this working with just two columns is:

london[(london['Events'].isnull() | london['Max Gust SpeedKm/h'].isnull())]

However, I need to achieve this result with all 23 columns, so I have attempted to complete this with some code.

Attempted Solution

  • Creating a string containing all of the column headers
  • i.e. london[(london['Column Header'].isnull() followed by | and then the next column
  • Then using this string within the container shown in the working example above
  • i.e. london[(string)]

I have managed to create the string I need using the following:

string = []
for i in (london.columns.values):
    string.append("london['" + i + "'].isnull()")
    string.append(" | ")
del string[-1]
final_string = "".join(string)

And finally when I try to implement the final step, I cannot work out how to convert this string into usable code.

For example:

now = eval(final_string)
london[now]

Resulting in:
NotImplementedError: 'Call' nodes are not implemented

Thank you in advance.

ihsancemil
string = []
for i in (london.columns.values):
    string.append(london[i].isnull())
london[0<sum(string)]

Since you will have only 1 and 0 and you are looking for at least one 1 then you can just add 1,0's to your list then sum them. if the sum is more than one your if will turn 1 otherwise your if will turn 0 so you can do london index after that.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I convert a string into bytes in python?

How do I convert this string into a dict in python?

How do I convert a string to math in python?

How do I convert this Matlab code with meshgrid and arrays to Python code?

How do I convert a cmake variable to a string in C++ code?

How do I convert a string into a string of bytes in python 2.7

How do I convert a string to a string literal in python?

how do I convert this python code in python function?

How do I convert this simple python code into javascript?

How to convert a dictionary value from a string to an int (Or how do I better write my code read it the value as an integer) in python

How do I convert a string to an escape sequence in Python?

How do I convert a list of ascii values to a string in python?

How do I convert a list into a string with spaces in Python?

How do I convert a number to a string in the same format as the input in python?

How do I convert a string from an input into an integer? (Python 3.9)

How do i convert a ascii python string to its trinary representation

How do I convert a list of integers to a string - Python

How do I convert a bytes object to a string in python?

How do I convert a string response size to MB in Python?

How do I execute a string containing Python code in Python?

How do I run a php code string from Python?

How can I convert string to internal code?

How do I convert this code using promises?

How do I convert this haml code to erb?

How do I convert this fetch response to a string?

How do I convert a string into HEX representation?

How do I convert a string to a number in PHP?

How do i convert a character array to a string?

How do I convert a String to a BlobColumn in SSIS