0

I am new to analyze using python, I wonder how can I transform the format of the left table to the right one. My initial thought is to create a nested for loop.

The desired table First, I find read the required csv file.

Imported csv

Then, I count the number of countries in the Column 'country' and the number of the new column names list.

`countries = len(test['country'])`
`columns = len(['Year', 'Values'])`

After that, I should go for the nested for loop, however, I have no idea on writing the code.What I have come up was as follows:

`for i in countries:`
  `for j in columns:`

2 Answers 2

2

You can use df.melt here:

In [3575]: df = pd.DataFrame({'country':['Afghanistan', 'Albania'], '1970':[1.36, 6.1], '1971':[1.39, 6.22], '1972':[1.43, 6.34]})

In [3576]: df
Out[3576]: 
       country  1970  1971  1972
0  Afghanistan  1.36  1.39  1.43
1      Albania  6.10  6.22  6.34

In [3609]: df = df.melt('country', var_name='Year', value_name='Values').sort_values('country')

In [3610]: df
Out[3610]: 
       country  Year  Values
0  Afghanistan  1970    1.36
2  Afghanistan  1971    1.39
4  Afghanistan  1972    1.43
1      Albania  1970    6.10
3      Albania  1971    6.22
5      Albania  1972    6.34
Sign up to request clarification or add additional context in comments.

5 Comments

@CheTou Please check my answer. Let me know if this is what you want.
not sure that this is what he wanted: i.sstatic.net/lH1hq.png
@dallonsi My output matches exactly the output pasted by OP in the image. What am I missing here?
oh right my mistake ! He starts with this table : i.sstatic.net/aOMu7.png I thought he wanted to go from the right table to the left table on this image i.sstatic.net/lH1hq.png .... !!
@Mayank Porwal Yes, this is what I want. And I have asked my friend the same question and he gave me another approach to get the same result
0

Not sure of what you want to do, but:

  • If you want to transform a column in a numpy array, you can use the following example:
import pandas as pd
import numpy as np

df = pd.DataFrame({"foo": [1,2,3], "bar": [10,20,30]})
print(df)

foo_array = np.array(df["foo"])
print(foo_array)

and then iterate on foo_array

  • You can also loop on your data frame using :
for row in df.iterrows():
    print(row)

But it's not recommended since you can often use built in pandas function to do the same job.

  • your data frame is also an iterable object which only contains the columns names:
for d in df:
    print(d)

# output:
# foo
# bar

1 Comment

I would like to change the columns' name in the original table to values in the new columns, just like this image, i.sstatic.net/lH1hq.png

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.