I have dataframe just like that:
df = pd.DataFrame({
'timestamp': ['2017-06-04 07:59:42', '2017-06-04 07:59:42',
'2017-06-04 07:59:42', '2017-06-04 07:59:42',
'2017-06-04 07:59:42'],
'municipality_id': [9, 8, 4, 0, 7],
'usage': [454, 556, 1090, 204, 718],
'total_capacity': [1332, 2947, 3893, 2813, 2019]
})
timestamp | municipality_id | usage | total_capacity |
---|---|---|---|
2017-06-04 07:59:42 | 9 | 454 | 1332 |
2017-06-04 07:59:42 | 8 | 556 | 2947 |
2017-06-04 07:59:42 | 4 | 1090 | 3893 |
2017-06-04 07:59:42 | 0 | 204 | 2813 |
2017-06-04 07:59:42 | 7 | 718 | 2019 |
So how can I get the first ten characters of every row of first column like:
0 2017-06-04
1 2017-06-04
2 2017-06-04
3 2017-06-04
4 2017-06-04
CodePudding user response:
From your csv file, there are multiple methods to extract the date
Method 1: parse_dates
from read_csv
:
df = pd.read_csv('data.csv', parse_dates=['timestamp'])
df['date'] = df['timestamp'].dt.date
Method 2: to_datetime
:
df = pd.read_csv('data.csv')
df['date'] = pd.to_datetime(df['timestamp']).dt.date
Method 3: str[]
df = pd.read_csv('data.csv')
df['date'] = df['timestamp'].str[:10]
The output of each method is:
>>> df['date']
1 2017-06-04
2 2017-06-04
3 2017-06-04
4 2017-06-04
...
13065 2017-08-19
13066 2017-08-19
13067 2017-08-19
13068 2017-08-19
13069 2017-08-19
Name: date, Length: 13070, dtype: object
CodePudding user response:
Method 4: str.split()
df = pd.read_csv('data.csv')
df['date'] = df['timestamp'].str.split(' ').str[0]