Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have a very large dataset(test) of approx 1 million rows. I want to update a column('Date') from the dataset. I just want 3 dates in my 'Date' column:

2014-04-01, 2014-05-01, 2014-06-01

So each date in one row and after every 3rd row dates are repeating.

I have tried this:

for i in range(0,len(test),3):

    if(i <= len(test)):

       test['Date'][i] = '2014-04-01'

       test['Date'][i+1] = '2014-05-01'

       test['Date'][i+2] = '2014-06-01'

I am getting this warning:

__main__:3: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
__main__:4: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
__main__:5: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy

I have gone through the link but could not able to solve my issue. And I have googled it, got some solutions like copy() dataset before slicing and some others but nothing worked.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
196 views
Welcome To Ask or Share your Answers For Others

1 Answer

I believe what you want is np.tile:

from math import ceil

dates = pd.Series(['2014-04-01', '2014-05-01', '2014-06-01'], dtype='datetime64[ns]')

repeated_dates = np.tile(dates, len(df) // 3 + 1)[:len(df)]

df['dates'] = repeated_dates

This creates a Series containing repeated values and assigns it to a column of your dataframe.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...