William.D
William.D

Reputation: 125

How to pandas rows into column?

I have a dataset that looks like this:

enter image description here

And I want to have a column that's called "Date" and has all the dates that I currently have as rows. Does anyone have any idea how to do that?

Upvotes: 1

Views: 61

Answers (2)

BENY
BENY

Reputation: 323226

In your case

df = df.rename_axis('Date').reset_index()

Upvotes: 1

Hammurabi
Hammurabi

Reputation: 1179

Your dataframe has the dates as an index, so reset index to pull them out into a column. If the index was previously named something other than 'index', use that name instead of 'index' if you choose to rename it.

df = pd.DataFrame([[2], [4]], columns=['order'])

df = df.reset_index()
df = df.rename(columns={'index':'date'})
print(df)
#    date  order
# 0     0      2
# 1     1      4

Upvotes: 1

Related Questions