Extract elements by Pandas column name x row number

Introduction

Although element extraction comes out based on row name / column name, row number / column number such as loc, iloc, etc. I couldn't find the extraction method by column name x row number, so I posted a memo.

What I wanted to do

From the original data frame (with column name, without row name), divide into groups with specific elements, It was necessary to refer to each data frame one line at a time for processing.

At that time, I tried to process by row number because I could specify the column but not the row as I expected. (I thought about reset_index, but I'm sure it should be possible to specify by column name x row number ...)

Conclusion

At and index can be combined and extracted with df.at [df.index [row number],'column name'] </ b>

(I should have read the article below properly ...) https://note.nkmk.me/python-pandas-at-iat-loc-iloc/

Trial

DataFrame


#Create a data frame with only column names
df = pd.DataFrame({'person':['a','b','c','a','b','c'],\
                  'name':['aa','bb','cc','aaa','bbb','ccc'],\
                  'num':[100,200,300,1000,2000,3000]})

	person	name	num
0	a	aa	100
1	b	bb	200
2	c	cc	300
3	a	aaa	1000
4	b	bbb	2000
5	c	ccc	3000

Either way is possible to simply extract the 0th row (= aa) of the "name" column from the original df.

person_0


df['name'][0]
#On the extracted line(Omoto)index=Can be extracted because there is 0
# aa

df.at[df.index[0],'name']
# aa

This time, narrow down the "person" column to the same person, and then try to get the 0th row.

  • Although it is different from this story, when narrowing down the matrix or extracting elements Be careful not to get addicted to "SettingWithCopyWarning" (I was addicted to it ...)   https://linus-mk.hatenablog.com/entry/2019/02/02/200000   https://qiita.com/HEM_SP/items/56cd62a1c000d342bd70

person_a_0


# "person"Narrow down to row with column a
df_a = df[df['person']=="a"]
df_a
	person	name	num
0	a	aa	100
3	a	aaa	1000

df_a['name'][0]
#On the extracted line(Omoto)index=Can be extracted because there is 0
# aa

df_a.at[df_a.index[0],'name']
# aa

person_b_0


# "person"Narrow down to row with column b
df_b = df[df['person']=="b"]
df_b
	person	name	num
1	b	bb	200
4	b	bbb	2000

df_b['name'][0]
#Index on the extracted row=Error because there is no 0

df_b.at[df_b.index[0],'name']
# bb

By using at, we were able to extract elements by line number for any data frame.

At the end

I hope it will be useful for those who are in trouble because they cannot find it.

Recommended Posts

Extract elements by Pandas column name x row number