Tag Archives: python for business analysts

Pandas Example – Write a Pandas program to merge two given dataframes with different columns

(Python Example for Beginners)   Write a Pandas program to merge two given dataframes with different columns.   Test Data: data1: key1 key2 P Q 0 K0 K0 P0 Q0 1 K0 K1 P1 Q1 2 K1 K0 P2 Q2 3 K2 K1 P3 Q3 data2: key1 key2 R S 0 K0 K0 R0 S0 …

Pandas Example – Write a Pandas program to combine the columns of two potentially differently-indexed DataFrames into a single result DataFrame

(Python Example for Beginners)   Write a Pandas program to combine the columns of two potentially differently-indexed DataFrames into a single result DataFrame.   Test Data: data1: A B K0 A0 B0 K1 A1 B1 K2 A2 B2 data2: C D K0 C0 D0 K2 C2 D2 K3 C3 D3   Sample Solution: Python Code …

Pandas Example – Write a Pandas program to join two dataframes using keys from right dataframe only

(Python Example for Beginners)   Write a Pandas program to join two dataframes using keys from right dataframe only.   Test Data: data1: key1 key2 P Q 0 K0 K0 P0 Q0 1 K0 K1 P1 Q1 2 K1 K0 P2 Q2 3 K2 K1 P3 Q3 data2: key1 key2 R S 0 K0 K0 …

Pandas Example – Write a Pandas program to join the two given dataframes along rows and merge with another dataframe along the common column id

(Python Example for Beginners)   Write a Pandas program to join the two given dataframes along rows and merge with another dataframe along the common column id.   Test Data: student_data1: student_id name marks 0 S1 Danniella Fenton 200 1 S2 Ryder Storey 210 2 S3 Bryce Jensen 190 3 S4 Ed Bernal 222 4 …

Pandas Example – Write a Pandas program to append a list of dictioneries or series to a existing DataFrame and display the combined data

(Python Example for Beginners)   Write a Pandas program to append a list of dictioneries or series to a existing DataFrame and display the combined data.   Test Data: student_id name marks 0 S1 Danniella Fenton 200 1 S2 Ryder Storey 210 2 S3 Bryce Jensen 190 3 S4 Ed Bernal 222 4 S5 Kwame …

Pandas Example – Write a Pandas program to remove repetitive characters from the specified column of a given DataFrame

(Python Example for Beginners)   Write a Pandas program to remove repetitive characters from the specified column of a given DataFrame.   Sample Solution: Python Code : import pandas as pd import re as re pd.set_option(‘display.max_columns’, 10) df = pd.DataFrame({ ‘text_code’: [‘t0001.’,’t0002′,’t0003′, ‘t0004’], ‘text_lang’: [‘She livedd a long life.’, ‘How oold is your father?’, ‘What …

Pandas Example – Write a Pandas program to extract only non alphanumeric characters from the specified column of a given DataFrame

(Python Example for Beginners)   Write a Pandas program to extract only non alphanumeric characters from the specified column of a given DataFrame.   Sample Solution: Python Code : import pandas as pd import re as re pd.set_option(‘display.max_columns’, 10) df = pd.DataFrame({ ‘company_code’: [‘c0001#’,’c00@0^2′,’$c0003′, ‘c0003’, ‘&c0004’], ‘year’: [‘year 1800′,’year 1700′,’year 2300’, ‘year 1900’, ‘year 2200’] …

Pandas Example – Write a Pandas program to extract only phone number from the specified column of a given DataFrame.

(Python Example for Beginners)   Write a Pandas program to extract only phone number from the specified column of a given DataFrame.   Sample Solution: Python Code : import pandas as pd import re as re pd.set_option(‘display.max_columns’, 10) df = pd.DataFrame({ ‘company_code’: [‘c0001′,’c0002′,’c0003’, ‘c0003’, ‘c0004’], ‘company_phone_no’: [‘Company1-Phone no. 4695168357′,’Company2-Phone no. 8088729013′,’Company3-Phone no. 6204658086’, ‘Company4-Phone no. …

Pandas Example – Write a Pandas program to extract only number from the specified column of a given DataFrame

(Python Example for Beginners)   Write a Pandas program to extract only number from the specified column of a given DataFrame.   Sample Solution: Python Code : import pandas as pd import re as re pd.set_option(‘display.max_columns’, 10) df = pd.DataFrame({ ‘company_code’: [‘c0001′,’c0002′,’c0003’, ‘c0003’, ‘c0004’], ‘address’: [‘7277 Surrey Ave.’,’920 N. Bishop Ave.’,’9910 Golden Star St.’, ’25 …

Pandas Example – Write a Pandas program to extract word mention someone in tweets using @ from the specified column of a given DataFrame

(Python Example for Beginners)   Write a Pandas program to extract word mention someone in tweets using @ from the specified column of a given DataFrame.   Sample Solution: Python Code : import pandas as pd import re as re pd.set_option(‘display.max_columns’, 10) df = pd.DataFrame({ ‘tweets’: [‘@Obama says goodbye’,’Retweets for @cash’,’A political endorsement in @Indonesia’, …