Skip to content

Tag: python

Trying to understand Python loop using underscore and input

One more tip – if anyone is learning Python on HackerRank, knowing this is critical for starting out. I’m trying to understand this code: Output: I put 2 as the first raw input. How does the function know that I’m only looping twice? This is throwing me off because it isn’t the typical…

Formatting output of CSV file in Python

I am creating a very rudimentary “Address Book” program in Python. I am grabbing contact data from a CSV file, the contents of which looks like the following example: I am trying to format the output so that it looks cleaner and more readable, i.e. everything lined up in rows and columns, like thi…

Pandas: convert dtype ‘object’ to int

I’ve read an SQL query into Pandas and the values are coming in as dtype ‘object’, although they are strings, dates and integers. I am able to convert the date ‘object’ to a Pandas datetime dtype, but I’m getting an error when trying to convert the string and integers. Here…

How can I use “e” (Euler’s number) and power operation?

How can I write 1-e^(-value1^2/2*value2^2) in Python? I don’t know how to use power operator and e. Answer You can use exp(x) function of math library, which is same as e^x. Hence you may write your code as: I have modified the equation by replacing 1/2 as 0.5. Else for Python <2.7, we’ll have …

how to add a package to sys path for testing

This question is occasioned by instructions in the python guide for adding a project to sys path to use in tests, which do not seem to work unless I am misunderstanding the instructions I have a directory structure for a python project like this According to the python guide, I should create a test/context.py…

Stack columns above value labels in pandas pivot table

Given a dataframe that looks like: Key1 Key2 Value1 Value2 0 one A 1.405817 1.307511 1 one B -0.037627 -0.215800 2 two C -0.116591 -1.195066 3 three A 2.044775 -1.207433 4 one B -1.109636 0.031521 5 one C -1.529597 1.761366 6 two A -1.349865 0.321454 7 three B 0.814374 2.285579 8 one C 0.178702 0.479210 9 one…