0

I have listA as

[{'index': 0,
  'keywords': ['nuclear','china','force','capabilities','pentagon','defence']},
 {'index': 1,
  'keywords': ['pakistan', 'membership', 'china', 'nsg', 'kirby', 'meets']},
 {'index': 2,
  'keywords': ['payment', 'rbi', 'applications', 'bill', 'bbpou', 'payments']}]

`

I want to add it into pandas column as first element into first cell. Output I'm expected as:-

Column_new
{'index': 0,'keywords': ['nuclear','china','force','capabilities','pentagon','defence']}
{'index': 1,'keywords': ['pakistan', 'membership', 'china', 'nsg', 'kirby', 'meets']}
{'index': 2,'keywords': ['payment', 'rbi', 'applications', 'bill', 'bbpou', 'payments']}

What I have done is :-

df = pd.DataFrame(listA,col=['Column_new'])

but it gives a dataframe with NaN values,

   Column_new
0  NaN

1  NaN

2  NaN
Aran-Fey
  • 39,665
  • 11
  • 104
  • 149
James
  • 528
  • 1
  • 6
  • 18

1 Answers1

2

The thing is... you got a series. Do this instead:

df = pd.Series(listA).to_frame('Column_new')

Full example:

import pandas as pd

listA = [{'index': 0,
  'keywords': ['nuclear','china','force','capabilities','pentagon','defence']},
 {'index': 1,
  'keywords': ['pakistan', 'membership', 'china', 'nsg', 'kirby', 'meets']},
 {'index': 2,
  'keywords': ['payment', 'rbi', 'applications', 'bill', 'bbpou', 'payments']}]


df = pd.Series(listA).to_frame('Column_new')
print(df)

Returns:

                                          Column_new
0  {'index': 0, 'keywords': ['nuclear', 'china', ...
1  {'index': 1, 'keywords': ['pakistan', 'members...
2  {'index': 2, 'keywords': ['payment', 'rbi', 'a...
Anton vBR
  • 18,287
  • 5
  • 40
  • 46
  • @James In that case you would do `df['newcolumn'] = pd.Series(listA)` or simply: `df['newcolumn'] = listA`. Or am I missing something? – Anton vBR Jul 07 '18 at 22:00
  • can you please look into this `https://stackoverflow.com/questions/51574485/match-keywords-in-pandas-column-with-another-list-of-elements`. I'm not getting the solution – James Jul 28 '18 at 20:49