2021年4月28日星期三

How can I group by a part of value in pandas?

I try to split and output the csv file. I must use the data to be the file name but don't need the time. So I want to split the Order_Date.
How can I group by a part of value in pandas? There is my code:

import csv  import re  import pandas as pd  import os    df = pd.read_csv('test.csv',delimiter='|')    for i,x in df.groupby('Order_Date'):      p = os.path.join(r'~/Desktop/',("data_{}.csv").format(i.lower()))      x.to_csv(p,sep = '|', index=False)    

Now I can get this:

data_2019-07-23 00:06:00.csv  data_2019-07-23 00:06:50.csv  data_2019-07-23 00:06:55.csv  data_2019-07-28 12:31:00.csv  

Example test.csv data:

Channel|Store_ID|Store_Code|Store_Type|Order_ID|Order_Date|Member_ID|Member_Tier|Coupon_ID|Order_Total|Material_No|Material_Name|Size|Quantity|Unit_Price|Line_Total|Discount_Amount  ECOM|ECOM|ECOM|ECOM|A190700|2019-07-23 00:06:00||||1064.00|7564|Full Zip|750|1.00|399.00|168.00|231.00  ECOM|ECOM|ECOM|ECOM|A190700|2019-07-23 00:06:00||||1064.00|1361|COOL TEE|200|1.00|199.00|84.00|115.00  ECOM|ECOM|ECOM|ECOM|A190700|2019-07-23 00:06:00||||1064.00|7699|PANT|690|1.00|499.00|210.00|289.00  ECOM|ECOM|ECOM|ECOM|A190700|2019-07-23 00:06:00||||1064.00|8700|AI DRESS|690|1.00|399.00|196.00|203.00  ECOM|ECOM|ECOM|ECOM|A190700|2019-07-23 00:06:50||||1064.00|8438|COPA|690|1.00|229.00|112.00|117.00  ECOM|ECOM|ECOM|ECOM|A190700|2019-07-23 00:06:55||||1064.00|8324|CLASS|350|1.00|599.00|294.00|305.00  ECOM|ECOM|ECOM|ECOM|A190701|2019-07-28 12:31:00||||798.00|3689|DRESS|500|1.00|699.00|294.00|405.00  

Expect I get this:

data_2019-07-23.csv  data_2019-07-28.csv  

Any help would be very much appreciated.

https://stackoverflow.com/questions/67310452/how-can-i-group-by-a-part-of-value-in-pandas April 29, 2021 at 11:02AM

没有评论:

发表评论