2

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.

3 Answers 3

3

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]
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, but more in general for other splits like names in one of column Reese, John split(",") how do I get the first name?
0

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.

Comments

0

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])

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.