warfo09
warfo09

Reputation: 157

Incrementing in Pandas

I am looking to simplify my code a bit when I fill in a a Pandas dataframe, for example this works great to auto-increment a string:

results['Cluster'] = ['Cluster %s' %i for i in range(1, len(results) + 1)]

Result:
Cluster 1
Cluster 2
Cluster 3 etc

How I do the same elegant code when I refer to another dataframe values, all I need to do is to increment cluster_X and total_cX, rather then refer to each one individually...

results['Item per Population'] = [cluster_1.shape[0]/total_c1,cluster_2.shape[0]/total_c2,cluster_3.shape[0]/total_c3]

Thank you.

Upvotes: 1

Views: 85

Answers (1)

warfo09
warfo09

Reputation: 157

ok, I got it myself.

First define

clusters = [cluster_1, cluster_2...etc)

totals_pop = [total_c1, total_c2...etc)

then:

appended_data = []
for i, x in zip(clusters,totals_pop):
 data = i.shape[0]/x
 appended_data.append(data)
appended_data
results['Car Wash per Population'] = np.array(appended_data)

There might be a more elegant way of doing this with results.loc[...] instead.

Upvotes: 1

Related Questions