Retrieve from a #Python #Pandas series by position, rather than the index, with iloc:

s = pd.Series([10, 20, 30], index=list('abc'))

s.loc['a'] # 10
s.loc[['a', 'b']] # series of a:10, b:20

s.iloc[0] # 10
s.iloc[[0, 1]] # series of a:10, b:20

Pass a list to .loc on a #Python #Pandas series, and get a series back:

s = pd.Series([10, 20, 30], index=list('abc'))

s.loc[['a']] # series, a:10
s.loc[['a', 'b']] # series, a:10, b:20
s.loc[['a', 'a']] # series, a:10 twice!
s.loc[['b', 'a']] # series, b:20, a:10

๐Ÿ”ฅ TRENDING

๐Ÿ“ข Is Pandas a Good Reason to Learn Python? - HackerNoon

๐Ÿ”— https://news.google.com/rss/articles/CBMib0FVX3lxTE9rT3VMOUVfT2szeVRJVjduamVVckFBYl9nZE5FcEFxWkh2dUFxVlFEWDltazVGcXBaODVpYWxUeWVCMjU5SkY3QnItbl9hOUdRd290OWhIZ0RmenVJQXROQzJ1em1fVkNVRXVGQmNlSQ?oc=5

#Pandas #Good #Reason #Learn #GlobalFeed #News #EN

<i>Automatically posted by Global Feed Bot</i>

๐Ÿš€ *The panel others don't want you to find.* ๐Ÿ‘ฅ Real Discord Members โค๏ธ Likes ๐Ÿ‘ Views ๐Ÿ‘ค Followers ๐Ÿ’ฐ Cheap Robux โ”โ”โ”โ”โ”โ”โ”...

Before you continue

Pandas 3: Call for help to spot the reason of failing tests

My current WIP: ``` diff @@ -4274,7 +4274,7 @@ (define-public python-tspex (define-public python-pandas (package (name "python-pandas") - (version "2.3.3") + (version "3.0.3") (source (origin (method git-fetch) @@ -4283,35 +4283,24 @@ (define-public py...

Codeberg.org

The index in a #Python #Pandas series feels like dict keys. But the keys can repeat:

s = pd.Series([10, 20, 30, 40], index=list('abca'))

s.loc['b'] # returns one value, np.int64(20)
s.loc['a'] # returns a series - both 'a' values, 10 and 40

To retrieve from a #Python #Pandas series, you can use [] to retrieve items.

But don't!

In a series, [] uses the index. In a data frame, [] uses the column names. Confusing!

Besides, .loc does everything [] does, but with more options and flexibility.

You can set a #Python #Pandas series index with set_axis. This returns a new series with the new index applied:

s = pd.Series([10, 20, 30], index=list('abc'))

(
s
.set_axis(list('qrs'))
.loc[['q', 's']]
) # q=10, s=30

s.index # still Index(['a', 'b', 'c'], dtype='str')

Want to change the index of a #Python #Pandas series? Just assign to it:

s = pd.Series([10, 20, 30], index=list('abc'))

s.index = list('xyz')
s.index # Index(['x', 'y', 'z'], dtype='str')

s.index = range(10, 15, 2)
s.index # RangeIndex(start=10, stop=15, step=2)

๐Ÿ’๐Ÿปโ€โ™€๏ธ TIL: ๐Ÿ”๏ธโ„๏ธ Roughly the size of #HongKong, #Sagarmatha National Park in north-east #Nepal contains Mount #Everest, the highest #mountain on #Earth.

Established in 1976 and declared a #UNESCO World Heritage Site in 1979, the #park spans 1,148 square km of #glaciers, river #valleys, and #forests. It hosts 28 #mammal species and over 150 #birds, including snow #leopards, red #pandas, Himalayan #tahr, and bar-headed #geese.

๐Ÿ‘‰ https://www.discoverwildlife.com/animal-facts/sagarmatha-national-park

#himalayas #everest #unesco #snowleopard #redpanda #wildlife #conservation #mountains #biodiversity #nature #geography

Itโ€™s the size of Hong Kong and contains the highest mountain on Earth โ€“ and itโ€™s home to a mighty carnivore | Discover Wildlife

Sagarmatha National Park in Nepal is home to some of the planet's hardiest animals โ€“ from woolly hares and red pandas to high-flying geese and stalking snow leopards.

Discover Wildlife

By default, a #Python #Pandas series has a RangeIndex, like a string or list:

s = pd.Series([10, 20, 30])
s.index # RangeIndex(start=0, stop=3, step=1)

Set an index like this:

s = pd.Series([10, 20, 30], index=list('abc'))
s.index # Index(['a', 'b', 'c'], dtype='str')