user3114645
user3114645

Reputation: 57

Python .transpose() gets error while transforming dictionary data

AttributeError: 'NoneType' object has no attribute 'transpose'

I have been trying to extract cells as dictionary(from pandas dataframe) and trying to join with existing data

for example , I have csv file which contains two columns id,device_type.each cell in device_type column contains dictionary data. i have trying to split and add with original data.

enter image description here

And trying to do something like below.

enter image description here

import json
import pandas

df = pandas.read_csv('D:\\1. Work\\csv.csv',header=0)
sf = df.head(12)
sf['visitor_home_cbgs'].fillna("{}", inplace = True).transpose()

-- csv file sample

ID,device_type
3c30ee03047b478,{"060379800281":11,"061110053031":5,"060372062002":5}
f5d639a64a88496099,{}

-- looks for output like below

id,device_type,ttype,tvalue
3c30ee03047b478,{"060379800281":11,"061110053031":5,"060372062002":5},"060379800281",11
3c30ee03047b478,{"060379800281":11,"061110053031":5,"060372062002":5},"061110053031",5
3c30ee03047b478,{"060379800281":11,"061110053031":5,"060372062002":5},"060372062002",5
f5d639a64a88496099,{},NIL,NIL

Upvotes: 0

Views: 1527

Answers (1)

Prince Francis
Prince Francis

Reputation: 3097

avoid inplace=True

sf['visitor_home_cbgs'].fillna("{}").transpose()

when you give inplace=True, it converts the same dataframe and returns null. If you want to use inplace=True, then do like below


sf['visitor_home_cbgs'].fillna("{}", inplace=True)
sf.transpose()

To create rows from column values

One solution it to iterate through dataframe rows and create new dataframe with desired columns and values.

import json
def extract_JSON(row):
    df2 = pd.DataFrame(columns=['ID', 'device_type', 'ttype', 'tvalue'])
    device_type = row['device_type']
    dict = json.loads(device_type)
    for key in dict:
        df2.loc[len(df2)] = [row['ID'], row['device_type'], key, dict[key]]
    if df2.empty:
        df2.loc[0] = [row['ID'], row['device_type'], '', '']
    return df2
df3 = pd.DataFrame(columns=['ID', 'device_type', 'ttype', 'tvalue'])
for _, row in df.iterrows():
    df3 = df3.append(extract_JSON(row))
df3

Upvotes: 1

Related Questions