Issue
I am working with a dataset like this, where the values of 'country name' are repeat several time, and 'Indicator name' to.
I want to create a new dataset with its columns are like that
Year CountryName IndicatorName1 IndicatorName2 ... IndicatorNameX
2000. USA. value1. value2. valueX
2000. Canada. value1. value2. valueX
2001. USA. value1. value2. valueX
2001. Canada. value1. value2. valueX
it is possible to do that??
Thanks in advances!
Solution
You can use pivot
as suggested by @Chris but you can also try:
out = df.set_index(['Country Name', 'Indicator Name']).unstack('Country Name').T \
.rename_axis(index=['Year', 'Country'], columns=None).reset_index()
print(out)
# Output
Year Country IndicatorName1 IndicatorName2
0 2000 France 1 3
1 2000 Italy 2 4
2 2001 France 5 7
3 2001 Italy 6 8
data = {'Country Name': ['France', 'Italy', 'France', 'Italy'],
'Indicator Name': ['IndicatorName1', 'IndicatorName1',
'IndicatorName2', 'IndicatorName2'],
2000: [1, 2, 3, 4],
2001: [5, 6, 7, 8]}
df = pd.DataFrame(data)
print(df)
# Output
Country Name Indicator Name 2000 2001
0 France IndicatorName1 1 5
1 Italy IndicatorName1 2 6
2 France IndicatorName2 3 7
3 Italy IndicatorName2 4 8
Answered By - Corralien
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.