एकाधिक डेटासेट को जोड़ना

pandas के साथ सरल Data Ingestion

Amany Mahfouz

Instructor

Concatenating

  • उपयोग: एक dataframe की पंक्तियाँ दूसरे में जोड़ना
  • concat()
    • pandas फंक्शन
    • सिंटैक्स: pd.concat([df1,df2])
    • पंक्तियों को फिर से नंबर देने के लिए ignore_index को True करें
pandas के साथ सरल Data Ingestion

Concatenating

# Get first 20 bookstore results
params = {"term": "bookstore", 
          "location": "San Francisco"}  
first_results = requests.get(api_url,
                             headers=headers,
                             params=params).json()

first_20_bookstores = json_normalize(first_results["businesses"],
                                     sep="_")

print(first_20_bookstores.shape)
(20, 24)
pandas के साथ सरल Data Ingestion
# Get the next 20 bookstores
params["offset"] = 20
next_results = requests.get(api_url,
                            headers=headers,
                            params=params).json()

next_20_bookstores = json_normalize(next_results["businesses"],
                                    sep="_")

print(next_20_bookstores.shape)
(20, 24)
pandas के साथ सरल Data Ingestion
# Put bookstore datasets together, renumber rows
bookstores = pd.concat([first_20_bookstores, next_20_bookstores],
                        ignore_index=True)

print(bookstores.name)
0                             City Lights Bookstore
1                            Alexander Book Company
2                                 Borderlands Books
3                                   Alley Cat Books
4                                   Dog Eared Books
...                                             ...
35                                     Forest Books
36                San Francisco Center For The Book
37                           KingSpoke - Book Store
38                            Eastwind Books & Arts
39                                      My Favorite
Name: name, dtype: object
pandas के साथ सरल Data Ingestion

Merging

  • उपयोग: संबंधित कॉलम जोड़ने के लिए डेटासेट जोड़ना
  • डेटासेट में समान मानों वाले key कॉलम होते हैं
  • merge(): SQL join का pandas संस्करण
pandas के साथ सरल Data Ingestion

Merging

  • merge()
    • pandas फंक्शन भी और dataframe मेथड भी
  • df.merge() के आर्ग्युमेंट्स
    • मर्ज करने के लिए दूसरा dataframe
    • किन कॉलम पर मर्ज करना है
      • दोनों dataframes में नाम एक जैसे हों तो on
      • key नाम अलग हों तो left_on और right_on
      • key कॉलम का data type एक जैसा होना चाहिए
pandas के साथ सरल Data Ingestion
call_counts.head()
  created_date  call_counts
0   01/01/2018         4597
1   01/02/2018         4362
2   01/03/2018         3045
3   01/04/2018         3374
4   01/05/2018         4333
weather.head()
         date    tmax  tmin  
0  12/01/2017      52    42  
1  12/02/2017      48    39  
2  12/03/2017      48    42  
3  12/04/2017      51    40  
4  12/05/2017      61    50
pandas के साथ सरल Data Ingestion

Merging

# Merge weather into call counts on date columns
merged = call_counts.merge(weather, 
                           left_on="created_date", 
                           right_on="date")

print(merged.head())
  created_date  call_counts        date  tmax  tmin
0   01/01/2018         4597  01/01/2018    19     7
1   01/02/2018         4362  01/02/2018    26    13
2   01/03/2018         3045  01/03/2018    30    16
3   01/04/2018         3374  01/04/2018    29    19
4   01/05/2018         4333  01/05/2018    19     9
pandas के साथ सरल Data Ingestion

Merging

   created_date  call_counts        date  tmax  tmin
0    01/01/2018         4597  01/01/2018    19     7
1    01/02/2018         4362  01/02/2018    26    13
2    01/03/2018         3045  01/03/2018    30    16
3    01/04/2018         3374  01/04/2018    29    19
4    01/05/2018         4333  01/05/2018    19     9
  • डिफॉल्ट merge() व्यवहार: केवल वे मान लौटाएँ जो दोनों डेटासेट में हों
  • dataframes के बीच हर वैल्यू मैच पर एक रिकॉर्ड
    • कई मैच = कई रिकॉर्ड
pandas के साथ सरल Data Ingestion

अभ्यास करते हैं!

pandas के साथ सरल Data Ingestion

Preparing Video For Download...