Python: append dictionary to pandas data frame row
Question:
I have a dictionary of following form:
{'2018': 21.6, '2019': 29.0, '2020': 134.8}
and a pandas dataframe of the following form
Index
Column1
Column2
Index1
Name1
URL1
Index2
Name2
URL2
Index3
Name3
URL3
my aim now is to append the dictionary to a fixed row, say the row with Index2. The result dataframe then should be:
Index
Column1
Column2
2018
2019
2020
Index1
Name1
URL1
Index2
Name2
URL2
21.6
29.0
134.8
Index3
Name3
URL3
after that I want append a second and third dictionary of same form into the rows with Index: Index1 and then Index3.
What is the best way to do that with python?
Answers:
I think best is first create new DataFrame by indices and dictionary and then add to original by DataFrame.join
:
d = {'2018': 21.6, '2019': 29.0, '2020': 134.8}
df = df.join(pd.DataFrame([d], index=['Index2']))
print (df)
Column1 Column2 2018 2019 2020
Index1 Name1 URL1 NaN NaN NaN
Index2 Name2 URL2 21.6 29.0 134.8
Index3 Name3 URL3 NaN NaN NaN
Or:
d = {'2018': 21.6, '2019': 29.0, '2020': 134.8}
df = df.join(pd.DataFrame.from_dict({'Index2': d}, orient='index'))
print (df)
Column1 Column2 2018 2019 2020
Index1 Name1 URL1 NaN NaN NaN
Index2 Name2 URL2 21.6 29.0 134.8
Index3 Name3 URL3 NaN NaN NaN
if you want to insert data into your dataframe with custom order you can try this:
import pandas as pd
df = pd.DataFrame([{'col1': 'Name1', 'col2': 'URL1'},
{'col1': 'Name2', 'col2': 'URL2'},
{'col1': 'Name3', 'col2': 'URL3'}],
index=['Index1','Index2','Index3'])
d2 = {'2018': 21.6, '2019': 29.0, '2020': 134.8}
d1 = {'2018': 200, '2019': 29.0, '2020': 134.8}
d3 = {'2018': 500, '2019': 29.0, '2020': 134.8}
df = df.join(pd.DataFrame([d2,d1,d3], index=['Index2','Index1','Index3']))
print (df)
Output:
col1 col2 2018 2019 2020
Index1 Name1 URL1 200.0 29.0 134.8
Index2 Name2 URL2 21.6 29.0 134.8
Index3 Name3 URL3 500.0 29.0 134.8
I have a dictionary of following form:
{'2018': 21.6, '2019': 29.0, '2020': 134.8}
and a pandas dataframe of the following form
Index | Column1 | Column2 |
---|---|---|
Index1 | Name1 | URL1 |
Index2 | Name2 | URL2 |
Index3 | Name3 | URL3 |
my aim now is to append the dictionary to a fixed row, say the row with Index2. The result dataframe then should be:
Index | Column1 | Column2 | 2018 | 2019 | 2020 |
---|---|---|---|---|---|
Index1 | Name1 | URL1 | |||
Index2 | Name2 | URL2 | 21.6 | 29.0 | 134.8 |
Index3 | Name3 | URL3 |
after that I want append a second and third dictionary of same form into the rows with Index: Index1 and then Index3.
What is the best way to do that with python?
I think best is first create new DataFrame by indices and dictionary and then add to original by DataFrame.join
:
d = {'2018': 21.6, '2019': 29.0, '2020': 134.8}
df = df.join(pd.DataFrame([d], index=['Index2']))
print (df)
Column1 Column2 2018 2019 2020
Index1 Name1 URL1 NaN NaN NaN
Index2 Name2 URL2 21.6 29.0 134.8
Index3 Name3 URL3 NaN NaN NaN
Or:
d = {'2018': 21.6, '2019': 29.0, '2020': 134.8}
df = df.join(pd.DataFrame.from_dict({'Index2': d}, orient='index'))
print (df)
Column1 Column2 2018 2019 2020
Index1 Name1 URL1 NaN NaN NaN
Index2 Name2 URL2 21.6 29.0 134.8
Index3 Name3 URL3 NaN NaN NaN
if you want to insert data into your dataframe with custom order you can try this:
import pandas as pd
df = pd.DataFrame([{'col1': 'Name1', 'col2': 'URL1'},
{'col1': 'Name2', 'col2': 'URL2'},
{'col1': 'Name3', 'col2': 'URL3'}],
index=['Index1','Index2','Index3'])
d2 = {'2018': 21.6, '2019': 29.0, '2020': 134.8}
d1 = {'2018': 200, '2019': 29.0, '2020': 134.8}
d3 = {'2018': 500, '2019': 29.0, '2020': 134.8}
df = df.join(pd.DataFrame([d2,d1,d3], index=['Index2','Index1','Index3']))
print (df)
Output:
col1 col2 2018 2019 2020
Index1 Name1 URL1 200.0 29.0 134.8
Index2 Name2 URL2 21.6 29.0 134.8
Index3 Name3 URL3 500.0 29.0 134.8