0
$\begingroup$

I have a data that looks like this: enter image description here The T2M indicates the temperature, and the followed row is the year. I want to append all the similar parameters columns under a single column having all the years, I will end up with one T2M column only, and the final dataframe would look like this

Parameter | T2M  | ...
Year      | 1981 | ...    
Jan
Feb 
.
.
Year      | 1982 | ...
.
.
.

I tried the following but it doesn't work:

dff = df.copy()
temp = df.iloc[:,1]
dff.append(temp)

I get this error :

ValueError: cannot reindex from a duplicate axis

which doesn't make sense because here in the first example similar indices were used.

$\endgroup$

1 Answer 1

1
$\begingroup$

Ok, I figured out the problem. The duplicate axis error was coming because the dataframe has multiple columns with the name 'T2M' so append() could not figure to which column it would append the new values.

Instead, I copied the dataframe, in the copy I deleted all columns to be appended, and extracted the data from the original df to the copied one. Since in the copy all columns are unique, everything went fine.

$\endgroup$

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.