Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas, print variable in string

I have a dataframe (new) that looks something like this:

num  name1  name2
11    A      AB
14    Y      YX
25    L      LS
39    Z      ZT
....

and I just want to extract the num value in a print statement such that I have an output that looks like this:

The value is 11
The value is 14
The value is 25
...

I'm not sure what the correct format to do this is, as the following bit of code just iterates "The value is".

 for index, row in new.iterrows():
     print('The value is').format(new['num'])
like image 441
EA00 Avatar asked May 07 '18 19:05

EA00


People also ask

How do you print a variable value in Python?

You first include the character f before the opening and closing quotation marks, inside the print() function. To print a variable with a string in one line, you again include the character f in the same place – right before the quotation marks.

What is %d and %s in Python?

%s is used as a placeholder for string values you want to inject into a formatted string. %d is used as a placeholder for numeric or decimal values.

How do you print the representation of a variable?

Python __str__() This method returns the string representation of the object. This method is called when print() or str() function is invoked on an object.

How to print variable and string in Python?

How to Print variable and string in python To print anything, a message, variable, or string in Python the print () function is used. The syntax to use a print keyword is different based on which version of python we are using it. The print function is consist of print keyword along with opening and closing paratheses (). print ("Hi,how are you!")

How to print a full string from a pandas Dataframe?

Another easier way to print the whole string is to call values on the dataframe. Show activity on this post. Just add the following line to your code before print. Show activity on this post. If you're using jupyter notebook, you can also print pandas dataframe as HTML table, which will print full strings.

How to print a variable having string without any other content?

Similarly to print a variable having string, without any other content To print this variable use the same method: As we know Python follows and what we call as Object Model so every number, string, data structure, function, class, module, and so on is considered as an Object.

How to insert a variable in a string in Python?

Method #2: using the "%" operator. By using the "%" operator, we can insert a variable wherever we want into a string. %s: for string. %d: for integer. Let's see how to use this method? variable = "Python" insert_var = "Hello %s I'm Pytutorial"%variable print(insert_var) Output:


2 Answers

Use str.join and f-strings

print('\n'.join(f'The value is {n}' for n in new.num))

The value is 11
The value is 14
The value is 25
The value is 39

A slight variant and more to show how to use the print function...

print(*(f'The value is {n}' for n in new.num), sep='\n')

The value is 11
The value is 14
The value is 25
The value is 39
like image 50
piRSquared Avatar answered Oct 15 '22 12:10

piRSquared


Slightly change your code

for index, row in df.iterrows():
    print('The value is {0}'.format(row['num']))
    
The value is 11
The value is 14
The value is 25
The value is 39

With f-strings:

for index, row in df.iterrows():
    print(f"The value is {row['num']}")

To print multiple columns, using dot notation:

for index, row in df.iterrows():
    print(f"{row.name1} and {row.name2} have a value of {row.num}")

A and AB have a value of 11
Y and YX have a value of 14
L and LS have a value of 25
Z and ZT have a value of 39
like image 25
BENY Avatar answered Oct 15 '22 14:10

BENY