Python Forum
Write specific rows from pandas dataframe to csv file - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Data Science (https://python-forum.io/forum-44.html)
+--- Thread: Write specific rows from pandas dataframe to csv file (/thread-13518.html)



Write specific rows from pandas dataframe to csv file - pradeepkumarbe - Oct-18-2018

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


RE: Write specific rows from pandas dataframe to csv file - volcano63 - Oct-18-2018

(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()



RE: Write specific rows from pandas dataframe to csv file - pradeepkumarbe - Oct-18-2018

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



RE: Write specific rows from pandas dataframe to csv file - volcano63 - Oct-18-2018

(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