Computer >> कंप्यूटर ट्यूटोरियल >  >> प्रोग्रामिंग >> Python

Python Pandas - DataFrame Property: shape का उपयोग कैसे करें

Products.csv file से data read करने और rows और columns की संख्या print करने के लिए Python program लिखें। फिर first ten rows के लिए 'product' column value 'Car' से match होने पर print करें।

मान लें, आपके पास 'products.csv' file है और rows और columns की संख्या और first ten rows के लिए 'product' column value 'Car' से match होने का result:

Products.csv file here download करें।

Rows: 100 Columns: 8
  id product engine avgmileage price height_mm width_mm productionYear
1  2   Car    Diesel     21      16500    1530      1735       2020
4  5   Car    Gas        18      17450    1530      1780       2018
5  6   Car    Gas        19      15250    1530      1790       2019
8  9   Car    Diesel     23      16925    1530      1800       2018

इस problem के लिए हमारे पास दो अलग-अलग solutions हैं।

Solution 1

  • Products.csv file से data read करें और df assign करें

df = pd.read_csv('products.csv ')
  • Rows की संख्या = df.shape[0] और columns = df.shape[1] print करें

  • df1 set करें df से first ten rows filter करने के लिए iloc[0:10,:] का उपयोग करके

df1 = df.iloc[0:10,:]
  • Product column values matches to car calculate करें df1.iloc[:,1] का उपयोग करके

यहां, product column index 1 है और finally data print करें

df1[df1.iloc[:,1]=='Car']

उदाहरण

बेहतर समझ के लिए निम्न code check करें:

import pandas as pd
df = pd.read_csv('products.csv ')
print("Rows:", df.shape[0], "Columns:", df.shape[1])
df1 = df.iloc[0:10,:]
print(df1[df1.iloc[:,1]=='Car'])

आउटपुट

Rows: 100 Columns: 8
  id product engine avgmileage price height_mm width_mm productionYear
1  2   Car    Diesel     21      16500    1530      1735       2020
4  5   Car    Gas        18      17450    1530      1780       2018
5  6   Car    Gas        19      15250    1530      1790       2019
8  9   Car    Diesel     23      16925    1530      1800       2018

Solution 2

  • Products.csv file से data read करें और df assign करें

df = pd.read_csv('products.csv ')
  • Rows की संख्या = df.shape[0] और columns = df.shape[1] print करें

  • df.head(10) का उपयोग करके first ten rows लें और df assign करें

df1 = df.head(10)
  • Product column values matches to Car लेने के लिए नीचे दी गई method का उपयोग करें

df1[df1['product']=='Car']

अब, आइए इसकी implementation check करें बेहतर समझ के लिए:

उदाहरण

import pandas as pd
df = pd.read_csv('products.csv ')
print("Rows:", df.shape[0], "Columns:", df.shape[1])
df1 = df.head(10)
print(df1[df1['product']=='Car'])

आउटपुट

Rows: 100 Columns: 8
  id product engine avgmileage price height_mm width_mm productionYear
1  2   Car    Diesel     21      16500    1530      1735       2020
4  5   Car    Gas        18      17450    1530      1780       2018
5  6   Car    Gas        19      15250    1530      1790       2019
8  9   Car    Diesel     23      16925    1530      1800       2018
  1. पायथन में पैंडास DataFrame को CSV फ़ाइल में कैसे लिखें पायथन में पैंडास DataFrame को CSV फ़ाइल में कैसे लिखें

    पायथन में पैंडास DataFrame को CSV फ़ाइल में लिखने के लिए to_csv() विधि का उपयोग करें। सबसे पहले, आइए सूचियों का एक शब्दकोश बनाएं:# सूचियों का शब्दकोश d = {Car: [BMW, Lexus, Audi, Mercedes, Jaguar, Bentley], Date_of_purchase: [2020-10-10, 2020-10-12, 2020-10-17, 2020-10-16, 2020-10-19, 2020-10-2

  1. पायथन - पैंडास DataFrame के सबसेट का चयन कैसे करें पायथन - पैंडास DataFrame के सबसेट का चयन कैसे करें

    मान लीजिए हमारी CSV फ़ाइल की सामग्री Microsoft Excel में निम्नलिखित है:सबसे पहले, CSV फ़ाइल से डेटा पैंडास DataFrame में लोड करें:dataFrame = pd.read_csv(C:\\Users\\amit_\\Desktop\\SalesData.csv)सबसेट का चयन करने के लिए वर्गाकार ब्रैकेट का उपयोग करें। ब्रैकेट में कॉलम का उल्लेख करें और पूरे डेटासेट

  1. पायथन - पैंडास डेटाफ्रेम को बार ग्राफ में कैसे प्लॉट करें पायथन - पैंडास डेटाफ्रेम को बार ग्राफ में कैसे प्लॉट करें

    मान लीजिए हमारी CSV फ़ाइल में निम्नलिखित सामग्री है: Car Reg_Price 0 BMW 2000 1 Lexus 1500 2 Audi 1500 3 Jaguar 2000 4 Mustang 1500 आवश्यक लाइब्रेरी आयात करें: import pandas as pd import matplotlib.pyplot as mp हमारी CSV फ़ाइल डेस्कटॉप पर है। CSV फ़ाइल से डेटा लोड करके पैंडास डेटाफ्रेम में डालें: d