Home > database >  Pandas reads in csv file and splits sentences in multiple columns because of value behind " �
Pandas reads in csv file and splits sentences in multiple columns because of value behind " �

Time:11-24

I have the following dictionary:

    {0: {0: "It's chic", 1: 'Samsung and Panasonic were overpriced.', 2: 'Others that compare are much more expensive.', 3: "Can't beat it at the price.", 4: 'I bought the more expensive case  for my 8.9 but it made my kindle very heavy.'}, 1: {0: " looks expensive but it's affordable", 1: nan, 2: nan, 3: nan, 4: nan}, 2: {0: ' what more can you want.', 1: nan, 2: nan, 3: nan, 4: nan}}

When i try to read this in with the following code: pd.DataFrame(dict)

I unfortunately get texts split up in three columns, instead of only 1. How can this be solved?

Edit: Preferably in the following format to be read in:

dict_2 = {0: {0: "It's chic looks expensive but it's affordable 
what more can you want.",
1: 'Samsung and Panasonic were overpriced.',
2: 'Others that compare are much more expensive.',
3: "Can't beat it at the price.",
4: 'I bought the more expensive case  for my 8.9 but it made my 
kindle very heavy.'}}

Thanks in advance.

CodePudding user response:

you can use:

from collections import defaultdict
dd = defaultdict(list)
for d in range (0,len(dictt)): # you can list as many input dicts as you want here
    for key, value in dictt[d].items():
        if value ==np.nan:
            pass
        else:
            dd[key].append(value)
for i in range(0,len(dd)):
    dd[i]=''.join([x for x in dd[i] if str(x) != 'nan'])
        
df=pd.DataFrame(data={'col1':dd})
print(df)
'''
    col1
0   It's chic looks expensive but it's affordable what more can you want.
1   Samsung and Panasonic were overpriced.
2   Others that compare are much more expensive.
3   Can't beat it at the price.
4   I bought the more expensive case  for my 8.9 but it made my kindle very heavy.

'''

or (better):

df = pd.DataFrame(dictt)
df2 = pd.Series(df.fillna('').values.tolist()).str.join('')
print(df2)
'''
    0
0   It's chic looks expensive but it's affordable what more can you want.
1   Samsung and Panasonic were overpriced.
2   Others that compare are much more expensive.
3   Can't beat it at the price.
4   I bought the more expensive case  for my 8.9 but it made my kindle very heavy.


'''
  • Related