Python | Панды Series.transform ()
Серия Pandas - это одномерный массив ndarray с метками осей. Этикетки не обязательно должны быть уникальными, но должны быть хешируемого типа. Объект поддерживает индексирование как на основе целых чисел, так и на основе меток и предоставляет множество методов для выполнения операций, связанных с индексом.
Pandas Series.transform() function Call func (the passed function) on self producing a Series with transformed values and that has the same axis length as self.
Syntax: Series.transform(func, axis=0, *args, **kwargs)
Parameter :
func : If a function, must either work when passed a Series or when passed to Series.apply
axis : Parameter needed for compatibility with DataFrame.
*args : Positional arguments to pass to func.
**kwargs : Keyword arguments to pass to func.Returns : Returns series that must have the same length as self.
Example #1: Use Series.transform() function to transform the elements of the given Series object. Append ‘_City’ at the end of each city name.
# importing pandas as pdimport pandas as pd # Creating the Seriessr = pd.Series(["New York", "Chicago", "Toronto", "Lisbon", "Rio", "Moscow"]) # Create the Datetime Indexdidx = pd.DatetimeIndex(start ="2014-08-01 10:00", freq ="W", periods = 6, tz = "Europe/Berlin") # set the indexsr.index = didx # Print the seriesprint(sr) |
Выход :

Now we will use Series.transform() function to append ‘_City’ at the end of each city name.
# append "_City"sr.transform(lambda x : x + "_City") |
Выход :

As we can see in the output, the Series.transform() function has successfully appended the desired keyword at the end of which city name.
Example #2: Use Dataframe.transform() function to transform the data of the given Dataframe. Increase the ticket cost of each even by 1000.
# importing pandas as pdimport pandas as pd # Creating the Dataframedf = pd.DataFrame({"Date":["10/2/2011", "11/2/2011", "12/2/2011", "13/2/2011"], "Event":["Music", "Poetry", "Theatre", "Comedy"], "Cost":[10000, 5000, 15000, 2000]}) # Print the dataframeprint(df) |
Выход :

Now we will use Dataframe.transform() function to increase the ticket cost by 1000
# transform the "Cost" columndf["Cost"] = df["Cost"].transform(lambda x : x + 1000) # Print the dataframe after modificationprint(df) |
Выход :

As we can see in the output, the Dataframe.transform() function has successfully increased the ticket cost of each event by 1000.
Внимание компьютерщик! Укрепите свои основы с помощью базового курса программирования Python и изучите основы.
Для начала подготовьтесь к собеседованию. Расширьте свои концепции структур данных с помощью курса Python DS. А чтобы начать свое путешествие по машинному обучению, присоединяйтесь к курсу Машинное обучение - базовый уровень.