1

I added a datetime to my data and I want to get rid of the old one, so it looks cleaner! Is it possible to change the title from data.dataset to something else?

I tried df.rename(columns={"data.datasets":"mydata"}) but unfortunately no changes :(

import datetime

#convert the column to datetime:
df["data.date"] = pd.to_datetime(df["data.date"])
df["date"] = df["data.date"].apply(lambda x : x.date())

df.date = pd.to_datetime(df.date)
df.set_index('date', inplace=True)

# First for the day:
df = df.resample('d').first()

# Print only 5/05 - 6/05
print(df.loc[df.index.to_series().between('2022-05-05', '2022-06-05')])

This is what the data currently looks like:

   data.date  data.datasets        Time       date                                                     
2022-05-05 2022-05-05 04:19:35         3.0500  2022-05-05
2022-05-06 2022-05-06 00:10:24         2.3500  2022-05-06
2022-05-07 2022-05-07 00:10:47         4.0900  2022-05-07
2022-05-08 2022-05-08 00:08:13         3.8790  2022-05-08
2022-05-09 2022-05-09 00:13:24         3.5780  2022-05-09
2022-05-10 2022-05-10 00:08:49         3.7500  2022-05-10
2022-05-11 2022-05-11 00:09:33         4.3900  2022-05-11
2022-05-12 2022-05-12 00:08:24         3.6900  2022-05-12
2022-05-13 2022-05-13 00:08:45         3.4300  2022-05-13
2022-05-14 2022-05-14 00:08:55         3.9500  2022-05-14
catlean
  • 25
  • 7
  • So basically do you just want to change the column names of the data frame? – Afsana Khan Jun 26 '22 at 19:58
  • @AfsanaKhan since i already added the time with datetime i'd prefer to remove the data.date on the left (repeats 2x i dont like the look of it) – catlean Jun 26 '22 at 20:05
  • Not important, but it looks like your sample output is missing a column header. – accdias Jun 26 '22 at 20:13
  • @AfsanaKhan, that is not quite right. The forum is exactly for this kind of question. OP could have search SO first, naturally, but the question is still **on-topic** here, even if it is a simple one. – accdias Jun 26 '22 at 20:22
  • @accdias These are duplicate questions. So according to the rules, the first task is to look for similar questions and only ask if the previous questions don't answer the question. – Afsana Khan Jun 26 '22 at 20:26
  • @AfsanaKhan, indeed. So, a better approach, would be indicating it is a duplicate and pointing OP to the right answer. – accdias Jun 26 '22 at 20:27
  • Does this answer your question? [Delete a column from a Pandas DataFrame](https://stackoverflow.com/questions/13411544/delete-a-column-from-a-pandas-dataframe) – accdias Jun 26 '22 at 20:29
  • my bad new to overflow and coding i sometimes don't know exactly what to search for, thought it'd be easier to just ask haha – catlean Jun 26 '22 at 20:29
  • @accdias it does but the renaming column code doesnt work :( not sure how to get aroudn that (df.rename(columns={"data.datasets":"mydata"}) – catlean Jun 26 '22 at 20:34
  • @AfsanaKhan i edited my question for clarification! – catlean Jun 26 '22 at 20:43
  • @catlean, take a look at [_Renaming column names in Pandas_](https://stackoverflow.com/questions/11346283/renaming-column-names-in-pandas). Use the search form above, in the header. – accdias Jun 26 '22 at 20:48
  • @AfsanaKhan i tried that code and it also didn't work. not sure if it's cause of my file, but the data.dataset and date aren't lined up together - it's above the data while the date is in line with the dates (if this explanation makes sense) – catlean Jun 26 '22 at 20:54

1 Answers1

1

Renaming the column(s) by providing a dictionary {}:

df.rename(columns={"data.datasets":"mydata"})

Removing a column:

df.drop(columns="data.date", inplace=True)
Chris Seeling
  • 606
  • 4
  • 11
  • hi!! the removal of a column code worked but renaming didn't :( any idea why? – catlean Jun 26 '22 at 20:33
  • use df.columns to get the exact column names, sometimes there can be a hard to spot typo or space. also use df.rename(columns={"data.datasets":"something"}, inplace="True") to edit inplace – Chris Seeling Jun 26 '22 at 21:02
  • i did inplace=true and it worked!! :3 thanks for the help – catlean Jun 26 '22 at 21:05