python pandas ham bosh va quyruqni tanlaydi

Pandas'dagi DataFrame uchun, birinchi 5 ta qiymatni va oxirgi 5 qiymatni qanday tanlash mumkin? Misol uchun

In [11]: df
Out[11]: 
        A  B  C
2012-11-29  0  0  0
2012-11-30  1  1  1
2012-12-01  2  2  2
2012-12-02  3  3  3
2012-12-03  4  4  4
2012-12-04  5  5  5
2012-12-05  6  6  6
2012-12-06  7  7  7
2012-12-07  8  8  8
2012-12-08  9  9  9

Birinchi qanot satrini va oxirgi tomosha satrlarini qanday ko'rsatish mumkin?

2
sizning savolingiz mantiqiy emas, siz birinchi 5 ta qiymat va oxirgi 5 qiymatlarni tanlashni xohlaysiz, siz satrlar yoki alohida qadriyatlar haqida gapirasizmi? Iltimos, kerakli chiqishni ko'rsating
qo'shib qo'ydi muallif EdChum, manba

6 javoblar

Siz iloc </<a href="https://docs.scipy.org/doc/numpy/reference/generated/numpy.r_.html" rel="noreferrer"> numpy.r _ :

print (np.r_[0:2, -2:0])
[ 0  1 -2 -1]

df = df.iloc[np.r_[0:2, -2:0]]
print (df)
            A  B  C
2012-11-29  0  0  0
2012-11-30  1  1  1
2012-12-07  8  8  8
2012-12-08  9  9  9

df = df.iloc[np.r_[0:4, -4:0]]
print (df)
            A  B  C
2012-11-29  0  0  0
2012-11-30  1  1  1
2012-12-01  2  2  2
2012-12-02  3  3  3
2012-12-05  6  6  6
2012-12-06  7  7  7
2012-12-07  8  8  8
2012-12-08  9  9  9
8
qo'shib qo'ydi
juda minnatdorman, bu men xohlagan!
qo'shib qo'ydi muallif fu xue, manba

Dastlabki 5 va oxirgi beshni olish uchun df.head (5) va df.tail (5) dan foydalanishingiz mumkin. Majburiy emas, siz yangi ma'lumotlar ramkasini va append() bosh va quyruq yaratishingiz mumkin:

new_df = df.tail(5)
new_df = new_df.append(df.head(5))
5
qo'shib qo'ydi

Kichik oddiy funksiya:

def ends(df, x=5):
    return df.head(x).append(df.tail(x))

Va shunga o'xshash foydalaning:

df = pd.DataFrame(np.random.rand(15,6))
ends(df,2)

Men aslida bu qadar ko'p narsani ishlataman, deb o'ylayman, bu pandaga qo'shilishning ajoyib xususiyati bo'lar edi.

2
qo'shib qo'ydi

ancha emas, balki faqat ko'rsatishni yuqori/pastki 5 satrlarni (masalan, display bilan jupyter yoki oddiy ni bosib chiqarishni istasangiz, pd.option_context kontekstida.

#make 100 3d random numbers
df = pd.DataFrame(np.random.randn(100,3))

# sort them by their axis sum
df = df.loc[df.sum(axis=1).index]

with pd.option_context('display.max_rows',10):
    print(df)

Chiqish ma'lumotlari:

           0         1         2
0  -0.649105 -0.413335  0.374872
1   3.390490  0.552708 -1.723864
2  -0.781308 -0.277342 -0.903127
3   0.433665 -1.125215 -0.290228
4  -2.028750 -0.083870 -0.094274
..       ...       ...       ...
95  0.443618 -1.473138  1.132161
96 -1.370215 -0.196425 -0.528401
97  1.062717 -0.997204 -1.666953
98  1.303512  0.699318 -0.863577
99 -0.109340 -1.330882 -1.455040

[100 rows x 3 columns]
1
qo'shib qo'ydi

df.head (2) va df.tail (2) dan foydalanishingiz mumkin.

1
qo'shib qo'ydi

Linas Fx bilan bog'langan.

Quyidagilarni belgilash

pd.DataFrame.less = lambda df, n=10: df.head(n//2).append(df.tail(n//2))

then you can type only df.less()

It's same as type df.head().append(df.tail())

If you type df.less(2), the result is same as df.head(1).append(df.tail(1))

0
qo'shib qo'ydi
Ba'zi formatlashlarni bajaring
qo'shib qo'ydi muallif Billa, manba
Python
Python
372 ishtirokchilar

Bu guruh python dasturlash tilini muhokama qilish uchun. Iltimos, o'zingizni hurmat qiling va faqat dasturlash bo'yicha yozing. Botlar mavzusini @botlarhaqida guruhida muhokama qling! FAQ: @PyFAQ Offtopic: @python_uz_offtopic

Python offtopic group !
Python offtopic group !
150 ishtirokchilar

@python_uz gruppasining offtop gruppasi. offtop bo'lsa ham reklama mumkin emas ) Boshqa dasturlash tiliga oid gruppalar @languages_programming