← Back to Pandas Course | Chapter 7: Grouping & Aggregation | Lesson 6 of 7

crosstab() का उपयोग

crosstab गिनता है कि category values की हर जोड़ी कितनी बार एक साथ आती है।

In this page:

  1. crosstab()
Syntax
python
pd.crosstab(df["column1"], df["column2"])
pd.crosstab(df["column1"], df["column2"], normalize="index")

crosstab()

pd.crosstab(a, b) दो columns की एक frequency table बनाता है। row proportions के लिए normalize="index" और totals के लिए margins=True जोड़ें। यह categorical variables के बीच संबंध study करने का तेज़ तरीका है।

Note: गिनने के बजाय किसी numeric column को aggregate करने के लिए crosstab को values और aggfunc पास करें।

उदाहरण: crosstab()

python
import pandas as pd

df = pd.DataFrame({"gender": ["F", "M", "F", "M", "F"], "plan": ["basic", "pro", "pro", "pro", "basic"]})
print(pd.crosstab(df["gender"], df["plan"]))
print(pd.crosstab(df["gender"], df["plan"], normalize="index").round(2))

# Output:
# plan    basic  pro
# gender
# F           2    1
# M           0    2
# plan    basic   pro
# gender
# F        0.67  0.33
# M        0.00  1.00
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. crosstab को pivot_table के साथ confuse करना
  2. यह भूल जाना कि normalize counts को shares में बदल देता है
  3. columns के बजाय पूरे DataFrames पास करना
चैप्टर सारांश
  • crosstab combinations गिनता है
  • normalize proportions देता है
  • margins totals जोड़ता है
  • Inputs columns या Series होते हैं
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.