Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dividing each row by the previous one

I have pandas dataframe:

df = pd.DataFrame()
df['city'] = ['NY','NY','LA','LA']
df['hour'] = ['0','12','0','12']
df['value'] = [12,24,3,9]

   city hour value
0   NY  0   12
1   NY  12  24
2   LA  0   3
3   LA  12  9

I want, for each city, to divide each row by the previous one and write the result into a new dataframe. The desired output is:

city ratio
NY   2
LA   3

What's the most pythonic way to do this?

like image 962
sato Avatar asked May 28 '18 09:05

sato


People also ask

How do you reverse a row in Python?

Using loc() function to Reverse Row Reversing the rows of a data frame in pandas can be done in python by invoking the loc() function. The panda's dataframe. loc() attribute accesses a set of rows and columns in the given data frame by either a label or a boolean array.

How do you transpose rows to columns in Python?

DataFrame - transpose() functionThe transpose() function is used to transpose index and columns. Reflect the DataFrame over its main diagonal by writing rows as columns and vice-versa. If True, the underlying data is copied. Otherwise (default), no copy is made if possible.

How do I turn a row into a column in pandas?

columns() to Convert Row to Column Header. You can use df. columns=df.

How do I swap rows and columns in pandas?

To swap the rows and columns of a DataFrame in Pandas, use the DataFrame's transpose(~) method.


1 Answers

First divide by shifted values per groups:

df['ratio'] = df['value'].div(df.groupby('city')['value'].shift(1))
print (df)
  city hour  value  ratio
0   NY    0     12    NaN
1   NY   12     24    2.0
2   LA    0      3    NaN
3   LA   12      9    3.0

Then remove NaNs and select only city and ratio column:

df = df.dropna(subset=['ratio'])[['city', 'ratio']]
print (df)
  city  ratio
1   NY    2.0
3   LA    3.0
like image 200
jezrael Avatar answered Sep 20 '22 16:09

jezrael