Basic Operations on Data using NumPy
FLATTENING TO THE DATAFRAME
Flattening of the Dataframe
Ankit is going to manage the event data. He is having the data as dataframe, and his manager wants to print those customers name who booked the event in the same month.
But the manager wants to print in the array format. So help Ankit to print customers name whose event month is same. And flatten the data and print in the array (list) format.
The following is the 2D format for the dataframe:
customer_name, address, date, eventType, cost, noOfPeople
Here eventType is described as marriage, get together, and mehndi.
Filled the data in the dataframe as the CSV file, in the above format.
Input Format:
For a sample dataframe, see the attached CSV file.
The following is the 2D format for the dataframe:
customer_name, address, date, eventType, cost, noOfPeople
Here eventType is described as marriage, get together, and mehndi.
Filled the data in the dataframe as the CSV file, in the above format.
Input Format:
For a sample dataframe, see the attached CSV file.
Output Format:
The output is the list of customers name with the month number of event booking.
Sample Input:
See the attached CSV file.
Sample Output:
Flatten Data:
['Vikram' '02' 'Amit' '02']
Description of the Sample Input/Output 1:
Sample Dataframe:
cust_name, addr, date, event_type, cost, no_of_people
Vikram,hebbal,27/02/2018,marriage,40000,400
Sachin,indore,18/05/2014,mehndi,70000,700
Amit,bhopal,20/02/2020,NaN,20000,200
Sumit,kanpur,10/12/2017,get together,10000,300
Vikram,hebbal,27/02/2018,marriage,40000,400
Sachin,indore,18/05/2014,mehndi,70000,700
Amit,bhopal,20/02/2020,NaN,20000,200
Sumit,kanpur,10/12/2017,get together,10000,300
Following is the dataframe of customer name with same month event:
Dataframe:
cust_name date
Vikram 02
Amit 02
Following is the flattening data of the above dataframe as list format:
Following is the flattening data of the above dataframe as list format:
Flatten Data:
['Vikram' '02' 'Amit' '02']
a=[]
with open('dataFrame1.csv', 'r') as f:
h=f.readline()
lines=f.readlines()
for line in lines:
a.append(line.rstrip().split(','))
mp=dict()
for row in a:
mon=row[2].split('/')[1]
if mon in mp:
mp[mon].append(row[0])
else:
mp[mon]=[row[0]]
for k in mp.keys():
if len(mp[k])>1:
res=''
for i in range(len(mp[k])):
res+=(' \'%s\' \'%s\'' % (mp[k][i], k))
res=res[1:]
res='['+res+']'
print(res)
Comments
Post a Comment