Python Forum

Full Version: Write specific rows from pandas dataframe to csv file
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Below is my CSV file

Territory  NoOfCustomer
D00060     10
D00061     20
D00065     70
D00067     90
I have to create a Unique Id based on Number of NoOfCustomer like If NoOfCustomer <=50
then I have to create 10 different Unique ID for Territory D00060 and 10 different Unique ID for Territory D00061.

Here I read my csv file in pandas like

csv_file = 'cust_valid.csv'
df=pd.read_csv(csv_file,delimiter="|")
Filtered having customers <= 50

low_dense = df['NoOfCustomer'] <=50
And then iterted low_dense like

for idx, item in df[low_dense].iterrows():
???

Now I will have to create 10 records means 10 unique ID using UUID for each territory having customers <=50? I am very new to python help me out!! Undecided
(Oct-18-2018, 07:48 PM)pradeepkumarbe Wrote: [ -> ]
for idx, item in df[low_dense].iterrows():
???

Now I will have to create 10 records means 10 unique ID using UUID for each territory having customers <=50? I am very new to python help me out!! Undecided

If you are new to Python - maybe, you should avoid pandas till you learn some basics. Still, pandas is aimed at table processing, meaning that iteration should be avoided when possible - mostly, it may be. Solution to you problem is very simple - if you know pandas a little bit
import uuid
df['UUID'] = uuid.uuid4()
Yes I agree UUID will solve my problem, but my output should be like below here how can we avoid Iteration?

Territory   NoOfCustomers    UniqueId 
D00060       10              0001ABSDFG79
D00060       10              0001ABKJKJ90
D00060       10              0001ABVVVV92
D00060       10              0001ABJHJJ93
D00060       10              0001ABCCVV95
(Oct-18-2018, 08:25 PM)pradeepkumarbe Wrote: [ -> ]Yes I agree UUID will solve my problem, but my output should be like below here how can we avoid Iteration?

Territory   NoOfCustomers    UniqueId 
D00060       10              0001ABSDFG79
D00060       10              0001ABKJKJ90
D00060       10              0001ABVVVV92
D00060       10              0001ABJHJJ93
D00060       10              0001ABCCVV95

You don't avoid iteration - you don't write it explicitly. pandas does it for you under the hood.

uuid.uuid<n>() yields one value on each call - yet you can assign its output to a DataFrame column. You can use any function on the right side of the assignment - or you may have a list/tuple, in the latter case the only limitation being that the length of the list macthes number of rows in your DataFrame