Reputation: 4375
I am trying to extract a index [1]
or month
from series but not getting it. Its series from a DataFrame.
x = ltest['Date'].str.split("-")
5659 [2015, 07, 26]
5696 [2015, 07, 26]
5783 [2015, 07, 26]
5833 [2015, 07, 26]
5836 [2015, 07, 26]
dtype: object
x[1] #error
x[x[1]] #error
ltest
Store DayOfWeek Date Sales Customers Open Promo StateHoliday SchoolHoliday
5659 85 7 2015-07-26 11421 1606 1 0 0 0
5696 122 7 2015-07-26 5773 707 1 0 0 0
5783 209 7 2015-07-26 3742 354 1 0 0 0
5833 259 7 2015-07-26 15998 2857 1 0 0 0
5836 262 7 2015-07-26 32547 4783 1 0 0 0
I am learning pandas. I checked api documentation but weren't able to figure out.
Upvotes: 2
Views: 3813
Reputation: 4375
I was missing the the parameter expand
so the split was returning list
perhaps not good for date extraction but, for string it will he helpful I think.
x = ltest['Date'].str.split(pat='-', expand=True)
x[1]
5659 07
5696 07
5783 07
5833 07
5836 07
Name: 1, dtype: object
Update:
ltest['Date'].map(lambda x: x[1])
Upvotes: 0
Reputation: 14849
Typically, you're better off keeping DataFrame columns as simple types rather than lists, dicts, etc. In this particular case, you can pull out specific elements from that list using apply though with something like x.apply(lambda x: x[1])
to pull the month, but Fabio's answer is better from a data organization perspective.
Upvotes: 0
Reputation: 21542
When reading your dataframe from file set the column Date
as datetime
:
df = pd.read_csv('yourfile.csv',parse_dates=['Date'])
In this way you can then access easily the information about the month:
df['Month'] = df['Date'].dt.month
This returns:
Store DayOfWeek Date Sales Customers Open Promo StateHoliday \
0 85 7 2015-07-26 11421 1606 1 0 0
1 122 7 2015-07-26 5773 707 1 0 0
2 209 7 2015-07-26 3742 354 1 0 0
3 259 7 2015-07-26 15998 2857 1 0 0
4 262 7 2015-07-26 32547 4783 1 0 0
SchoolHoliday Month
0 0 7
1 0 7
2 0 7
3 0 7
4 0 7
Then if you need the array of the Month
column you can get it with:
df['Month'].values
that returns:
[7 7 7 7 7]
Upvotes: 3