Question

time_period    total_cost    total_revenue
7days          150           250
14days         350           600
30days         900           750
7days          180           400
14days         430           620

Given this data, I want to convert the total_cost and total_revenue columns into averages for their given time period. I thought this would work:

df[['total_cost','total_revenue']][df.time_period]=="7days"]=df[['total_cost','total_revenue']][df.time_period]=="7days"]/7

But it returns the dataframe unchanged.

Was it helpful?

Solution

I believe that you are operating on copies of the dataframe. I think you should use apply:

from StringIO import StringIO
import pandas
datastring = StringIO("""\
time_period    total_cost    total_revenue
7days          150           250
14days         350           600
30days         900           750
7days          180           400
14days         430           620
""")

data = pandas.read_table(datastring, sep='\s\s+')

data['total_cost_avg'] = data.apply(
    lambda row: row['total_cost'] / float(row['time_period'][:-4]), 
    axis=1
)

gives me:

  time_period  total_cost  total_revenue  total_cost_avg
0       7days         150            250       21.428571
1      14days         350            600       25.000000
2      30days         900            750       30.000000
3       7days         180            400       25.714286
4      14days         430            620       30.714286

OTHER TIPS

Excellent answer by Paul. Adding my approach here

test_df = pd.read_csv("file1.csv")
test_df

   time_period      total_cost   total_revenue
0    7days          150        250
1    14days         350        600
2    30days         900        750
3    7days          180        400
4    14days         430        620

test_df['days'] = test_df.time_period.str.extract('(\d*)days').apply(int)
test_df['total_cost'] = test_df.total_cost / test_df.days
test_df['total_revenue'] = test_df.total_revenue / test_df.days
del test_df['days']
test_df


   time_period   total_cost       total_revenue
0    7days       21.428571          35.714286
1    14days      25.000000          42.857143
2    30days      30.000000          25.000000
3    7days       25.714286          57.142857
4    14days      30.714286          44.285714
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top