Python Forum
Write specific rows from pandas dataframe to csv file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Write specific rows from pandas dataframe to csv file
#1
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
Reply
#2
(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()
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply
#3
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
Reply
#4
(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
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  HTML Decoder pandas dataframe column mbrown009 3 961 Sep-29-2023, 05:56 PM
Last Post: deanhystad
  Errors if an xlsx file has blank rows in the beginning…. tester_V 1 790 Aug-28-2023, 06:22 PM
Last Post: deanhystad
  Use pandas to obtain cartesian product between a dataframe of int and equations? haihal 0 1,090 Jan-06-2023, 10:53 PM
Last Post: haihal
Smile How to further boost the data read write speed using pandas tjk9501 1 1,227 Nov-14-2022, 01:46 PM
Last Post: jefsummers
  Split excel file and write output at specific row and set sheet position DSCA 0 1,956 May-12-2022, 07:29 PM
Last Post: DSCA
  export dataframe to file.txt dramauh 5 1,884 Apr-21-2022, 01:23 AM
Last Post: sarahroxon7
  Pandas Dataframe Filtering based on rows mvdlm 0 1,396 Apr-02-2022, 06:39 PM
Last Post: mvdlm
  Pandas dataframe: calculate metrics by year mcva 1 2,266 Mar-02-2022, 08:22 AM
Last Post: mcva
  Pandas dataframe comparing anto5 0 1,241 Jan-30-2022, 10:21 AM
Last Post: anto5
  PANDAS: DataFrame | Replace and others questions moduki1 2 1,758 Jan-10-2022, 07:19 PM
Last Post: moduki1

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020