Python Forum
How to filter out Column data From Multiple rows data?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to filter out Column data From Multiple rows data?
#7
(Feb-02-2021, 10:02 PM)eddywinch82 Wrote: Hi firaki12345,

Here is a more cleaned up output version, of the previous Code :-

I hope you like it ))

import json
import pandas as pd
import re     
      
with open("walmart json file.txt") as f:
 data = json.load(f)
      
walmart = data["items"]
      
      
wdf = pd.DataFrame(walmart,columns=["productId","primaryOffer"])
      
      
print(wdf.loc[0,"primaryOffer"])
      
      
pd.set_option('display.max_colwidth', None)
      

wdf['primaryOffer'] = wdf['primaryOffer'].astype(str)

wdf['primaryOffer'] = wdf.primaryOffer.str.replace("'", '')

wdf['primaryOffer'] = wdf.primaryOffer.str.replace(",", '')

wdf['primaryOffer'] = wdf.primaryOffer.str.replace("{", '') 


wdf['offerId'] = wdf.primaryOffer.str.extract('.*offerId:\s?(\w+)', expand = False)
wdf['minPrice(USD)'] = wdf.primaryOffer.str.extract('.*minPrice:\s?(\d+[.]?\d*)', expand = False)
wdf['maxPrice(USD)'] = wdf.primaryOffer.str.extract('.*maxPrice:\s?(\d+[.]?\d*)', expand = False)
wdf['offerPrice(USD)'] = wdf.primaryOffer.str.extract('.*offerPrice:\s?(\d+[.]?\d*)', expand = False)

wdf=wdf.fillna('--')

wdf.drop('primaryOffer', axis=1, inplace=True)

wdf
Regards

Eddie Winch


hi very very sorry for the late reply, but as i said i found an easier solution by using json_normalize
as it turns all jumbled up data into columns. so it becomes easy to use in pandas :)
Reply


Messages In This Thread
RE: How to filter out Column data From Multiple rows data? - by firaki12345 - Feb-05-2021, 02:43 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Help with to check an Input list data with a data read from an external source sacharyya 3 527 Mar-09-2024, 12:33 PM
Last Post: Pedroski55
  Filter data into new dataframe as main dataframe is being populated cubangt 8 1,115 Oct-23-2023, 12:43 AM
Last Post: cubangt
  Returning Column and Row Data From Spreadsheet knight2000 0 490 Oct-22-2023, 07:07 AM
Last Post: knight2000
  how do you style data frame that has empty rows. gsaray101 0 559 Sep-08-2023, 05:20 PM
Last Post: gsaray101
  Database that can compress a column, or all data, automatically? Calab 3 1,280 May-22-2023, 03:25 AM
Last Post: Calab
  Code for pullng all data in a column EmBeck87 5 1,195 Apr-03-2023, 03:43 PM
Last Post: deanhystad
  (Python) Pulling data from UA Google Analytics with more than 100k rows into csv. Stockers 0 1,292 Dec-19-2022, 11:11 PM
Last Post: Stockers
  How to properly format rows and columns in excel data from parsed .txt blocks jh67 7 2,005 Dec-12-2022, 08:22 PM
Last Post: jh67
  Write sql data or CSV Data into parquet file mg24 2 2,529 Sep-26-2022, 08:21 AM
Last Post: ibreeden
  How to assign a value to pandas dataframe column rows based on a condition klllmmm 0 887 Sep-08-2022, 06:32 AM
Last Post: klllmmm

Forum Jump:

User Panel Messages

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