Reputation: 19
import pandas as pd
tabel = [{'192.168.70.150': '30'},
{'192.168.72.15': '38'},
{'192.168.72.150': '29'}]
df = pd.DataFrame(tabel)
print df
The output is:
192.168.70.150 192.168.72.15 192.168.72.150
0 30 NaN NaN
1 NaN 38 NaN
2 NaN NaN 29
But I want to be like:
192.168.70.150 30
192.168.72.15 38
192.168.72.150 29
What do I need to change in the code?
Upvotes: 0
Views: 39
Reputation: 862611
If possible dupicated ip
, better is create tuple
s in list comprehension
:
tabel = [{'192.168.70.150': '30'},
{'192.168.72.15': '38'},
{'192.168.72.150': '29'},
{'192.168.72.150': '20'}]
L = [(a, b) for d in tabel for a, b in d.items()]
df = pd.DataFrame(L, columns=['a','b'])
print (df)
a b
0 192.168.70.150 30
1 192.168.72.15 38
2 192.168.72.150 29
3 192.168.72.150 20
Upvotes: 0
Reputation: 267
When you create df from dictionary, then the keys will be columns and the values will be rows. Use list for your goals.
>>> import pandas as pd
>>> tabel = [['192.168.70.150', '30'],
... ['192.168.72.15', '38'],
... ['192.168.72.150', '29']]
>>> df = pd.DataFrame(table, columns=['IP', 'Value'])
>>> df
IP Value
0 192.168.70.150 30
1 192.168.72.15 38
2 192.168.72.150 29
Upvotes: 1
Reputation: 323226
You need stack
df.stack()
Out[349]:
0 192.168.70.150 30
1 192.168.72.15 38
2 192.168.72.150 29
dtype: object
Or you can flatten your list of dict to a dict , then using pd.Serise
df = pd.Series({k: v for d in tabel for k, v in d.items()})
df
Out[353]:
192.168.70.150 30
192.168.72.15 38
192.168.72.150 29
dtype: object
Upvotes: 1