Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

flat-table

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

flat-table

A broader implementation of pandas json_normalize function.

  • 1.1.1
  • PyPI
  • Socket score

Maintainers
1

Flat-Table: Dictionary and List Normalizer

This package is a normalizer for pandas dataframe objects that has dictionary or list objects within it's columns. The library will expand all of the columns that has data types in (list, dict) into individual seperate rows and columns.

PS: Flat table will use the current index of the dataframe as an identifier while expanding lists. The output will have an index column of your original dataframe. You can drop it later if you not plan to use it.

To Install

To install, use pip.

pip install flat-table

How to Use It

From a given pandas dataframe, the index of the dataframe will be used to create seperate columns and rows.

# some dataframe contains dicts and lists in it's columns
df = ...
import flat_table

flat_table.normalize(df)

This will give you all the keys in dictionaries as columns, and all the lists as seperate rows.

Example Illustration

Lets assume that you have a dataframe of the followings shape.

iduser_infoaddress
1001{ 'first_name': 'john', 'last_name': 'smith', 'phones': {'mobile': '201-..', 'home': '978-..'} }[{ 'zip': '07014', 'city': 'clifton' }]
1002NaN[{'zip': '07014', 'address1': '1 Journal Square'}]
1003 { 'first_name': 'marry', 'last_name': 'kate', 'gender': 'female' } [{ 'zip': '10001', 'city': 'new york' }, { 'zip': '10008', 'city': 'brooklyn' }]

This table given above has some dictionaries and lists in it's columns. Normally, what you would do is to use pd.io.json.json_normalize function to expand dictionaries. However, in cases you have NaN values in your column, pd.io.json.json_normalize end up throwing an AttributeError error for NaN values because they are not of the same type. flat_table is a wraper around the json_normalize function where it expands it's abilities to be more robust for NaN values and also, it expands lists rowwise so that it will be more clear to see the information.

For the above table, the flatten table after applying flat_table.normalize will look like the following.

indexiduser_info.genderuser_info.phones.homeuser_info.phones.mobileuser_info.last_nameuser_info.first_nameaddress.address1address.cityaddress.zip
001001nan978-..201-..smithjohnnanclifton07014
111002nannannannannan1 Journal Squarenan07014
221003femalenannankatemarrynannew york10001
321003femalenannankatemarrynanbrooklyn10008

New in Version 1.1.0

The expansion for dicts and lists made optional. Now, you can choose to expand list types and dict types with normalize function.

flat_table.normalize(df, expand_dicts=False, expand_lists=True)

Normalized version of df will be following.

indexiduser_infoaddress.address1address.cityaddress.zip
001001{...}nanclifton07014
111002nan1 Journal Squarenan07014
221003{...}nannew york10001
321003{...}nanbrooklyn10008

How it Works?

Basically, flat_table will look for each of the series in a dataframe to understand what type of data it contains.

For every series, it creates a list of information on how to expand it. It will go into all dictionaries and all lists in all levels and expand them as rows and columns. Dictionary keys will be used for column names, and The index of the giden dataframe will be used for row expansion.

If you want to see how the columns are mapped, you can use flat_table.mapper function to get all information about your columns in your original dataframe. For example, for the above table, the mapper function will provide the following table.

parentchildtypeobj
0.idint...
1.user_infodict...
2user_infouser_info.genderstr...
3user_infouser_info.phones.homestr...
4user_infouser_info.phones.mobilestr...
5user_infouser_info.last_namestr...
6user_infouser_info.first_namestr...
7.addresslist...
8addressdict...
9addressaddress.address1str...
10addressaddress.citystr...
11addressaddress.zipstr...

Licence

Licence is use it at your own will, with whatever way you want it to use :smiley:.

Author

Build by @metinsenturk

Keywords

FAQs


Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc