Home > front end >  Get the first 2 items from a column to make another Column Pandas
Get the first 2 items from a column to make another Column Pandas

Time:04-19

I have a table with a DateTime Column as shown below. The time Interval is in hours

ID  TimeInterval   Temperature
1   00:00:00            27
2   01:00:00            26
3   02:00:00            24
4   03:00:00            24
5   04:00:00            25

I tried to use the time interval for plotting. However, I got an error float() argument must be a string or a number, not 'datetime.time'

So, I want to extract the first two numbers of Column TimeInterval and have it into a new column. Any ideas how to extract that?

CodePudding user response:

You can use strftime with %H for hours

# Create test data
df = pd.DataFrame({'ID': [1, 2, 3, 4, 5], 'TimeInterval': ['00:00:00', '01:00:00', '02:00:00', '03:00:00', '04:00:00'], 'Temperature': [27, 26, 24, 24, 25]})

# Change the format to %H:%M:%S.
df['TimeInterval'] = pd.to_datetime(df['TimeInterval'], format='%H:%M:%S')

# Create a new column
df['new'] = df['TimeInterval'].dt.strftime('%H')

output:

ID  TimeInterval            Temperature new
1   1900-01-01 00:00:00     27          00
2   1900-01-01 01:00:00     26          01
3   1900-01-01 02:00:00     24          02
4   1900-01-01 03:00:00     24          03
5   1900-01-01 04:00:00     25          04

CodePudding user response:

If the "TimeInterval" column is a string ... you can select the first 2 characters from it and parse it later on into an integer:

df["new"] = df["TimeInterval"].str[:2].astype(int)

But that would be an ugly solution.

Here is a better solution

# test data
df = pd.DataFrame([{'TimeInterval':'00:00:00'}, 
                   {'TimeInterval':'01:00:00'}])

# cast it to a datetime object
df['TimeInterval'] = pd.to_datetime(df['TimeInterval'], format='%H:%M:%S')

# select the hours
df['hours'] = df['TimeInterval'].dt.hour
  • Related