pandas

29 items tagged with "pandas"

29 Articles

Articles

Geoanalytics with geopandas and ABS Public Data in Python

2024-04-06
Python Programming

Pandas DataFrame aggregate a list column to a set

This code snippet shows you how to group a pandas DataFrame and then aggregate a column with list or array type to a set (with duplicates removed) or a list. To implement it, we can first expode the column (list type) and then use groupby to create a grouped DataFrame and then aggregate using set or list or a combination of both. Input `` category users 0 A [1, 2] 1 B [3, 4] 2 C [5, 6, 7] 3 A [1, 8, 1] 4 B [1, 6, 9] ` Output ` category usersset userslist 0 A {8, 1, 2} [8, 1, 2] 1 B {1, 3, 4, 6, 9} [1, 3, 4, 6, 9] 2 C {5, 6, 7} [5, 6, 7] ``

2023-08-10
Python Programming

Union two pandas DataFrame

This code snippet shows you how to union two pandas DataFrames in python using concat method in pandas namespace.If the schema is different, pandas will autmatically merge it. Output `` category value user 0 A 0 2.0 1 B 1 3.0 2 C 2 2.0 3 D 3 1.0 4 E 4 1.0 0 A 0 NaN 1 B 1 NaN 2 C 2 NaN 3 D 3 NaN 4 E 4 NaN ` For the second DataFrame, column user doesn't exist. Pandas uses NaN` to mark it.

2023-08-09
Code Snippets & Tips

Pandas DataFrame groupBy and then COUNT DISTINCT

This code snippet shows you how to group a pandas DataFrame via certain column and then do a distinct count of unique values in another column. It is similar as COUNT DISTINCT aggregation functions in SQL. It also sort the values by the group by column. Example output: `` category value user group-count 0 A 0 5 7 80 A 80 4 7 70 A 70 7 7 60 A 60 10 7 50 A 50 9 7 .. ... ... ... ... 29 J 29 9 7 19 J 19 9 7 9 J 9 9 7 89 J 89 8 7 99 J 99 7 7 [100 rows x 4 columns] ``

2023-08-09
Code Snippets & Tips

Azure App Service IIS Log Analytics using Pandas

2022-09-05
Python Programming

Flatten Pandas DataFrame after Aggregate

In code snippet Pandas DataFrame Group by one Column and Aggregate using MAX, MIN, MEAN and MEDIAN, it shows how to do aggregations in a pandas DataFrame. This code snippet shows you how to flatten the DataFrame (multiindex) after aggregations. Sample output: category value\max value\min value\mean value\median 0 A 90 0 45 45 1 B 91 1 46 46 2 C 92 2 47 47 3 D 93 3 48 48 4 E 94 4 49 49 5 F 95 5 50 50 6 G 96 6 51 51 7 H 97 7 52 52 8 I 98 8 53 53 9 J 99 9 54 54

2022-09-04
Code Snippets & Tips

Pandas DataFrame Group by one Column and Aggregate using MAX, MIN, MEAN and MEDIAN

This code snippet provides one example of grouping a pandas DataFrame by one column and then aggregating on multiple columns using different functions including max, min, mean and median. We pass in a dictionary for each column that needs to be aggregated: the key is the column name, and the value is a list of aggregation functions supported by pandas DataFrame. The result DataFrame will have multiple levels as following output shows: Sample output: value max min mean median category A 90 0 45 45 B 91 1 46 46 C 92 2 47 47 D 93 3 48 48 E 94 4 49 49 F 95 5 50 50 G 96 6 51 51 H 97 7 52 52 I 98 8 53 53 J 99 9 54 54

2022-09-04
Code Snippets & Tips

Get First Top N Rows in a Pandas DataFrame

Method pandas.DataFrame.head can be used to retrieve top N records from a DataFrame object. It has one optional parameter for the number of rows to return; the default value is 5 if not specified. Syntax `` DataFrame.head(n=5) `` Sample output: A B 0 0.000000 0.000000 1 1.010101 10.101010 2 2.020202 20.202020 3 3.030303 30.303030 4 4.040404 40.404040 5 5.050505 50.505051 6 6.060606 60.606061 7 7.070707 70.707071 8 8.080808 80.808081 9 9.090909 90.909091

2022-09-04
Code Snippets & Tips

Check Column Data Types in a Pandas DataFrame

This code snippet provide examples of checking column data types in a DataFrame using dtype and dtypes. Sample output: >>> print(df.dtypes) a int64 b object dtype: object >>> print(df.a.dtype) int64

2022-09-04
Code Snippets & Tips

Rename Columns in Pandas DataFrame

This code snippet provides a simple approach to rename column names in a Panda DataFrame. It directly sets DataFrame.columns property. Output: !2022090404325-image.png

2022-09-04
Code Snippets & Tips

Pandas DataFrame - Get Row and Column Count

2022-07-07
Code Snippets & Tips

Pandas DataFrame - Iterate over Rows

2022-06-14
Code Snippets & Tips

Python: Read Data from Oracle Database

2022-06-05
Python Programming

Pandas DataFrame Plot Heatmap using Seaborn

2022-06-04
Python Programming

Python: Read Data from BigQuery

2021-03-17
Google Cloud Platform

Pandas - Save DataFrame to BigQuery

2021-03-17
Google Cloud Platform

Pandas Save DataFrame to SQLite

2021-02-10
Code Snippets & Tips

Convert Pandas DataFrame to Spark DataFrame

2021-01-24
Code Snippets & Tips

Create Pandas DataFrame from MySQL

2021-01-23
Code Snippets & Tips

Python: Load Data from MySQL

2021-01-23
Python Programming

Pandas Read from SQLite Database

2020-04-16
Python Programming

Pandas DataFrame Plot - Scatter and Hexbin Chart

2020-04-04
Code Snippets & Tips

Pandas DataFrame Plot - Area Chart

2020-04-04
Code Snippets & Tips

Pandas DataFrame Plot - Pie Chart

2020-04-04
Code Snippets & Tips

Pandas DataFrame Plot - Line Chart

2020-04-04
Code Snippets & Tips

Pandas DataFrame Plot - Bar Chart

2020-04-04
Code Snippets & Tips

Improve PySpark Performance using Pandas UDF with Apache Arrow

2019-12-29
Spark & PySpark

Create and Read Pickle Files in Python

2019-12-26
Code Snippets & Tips

Convert PySpark Row List to Pandas Data Frame

2019-08-22
Spark & PySpark