[Solved] Aggregate symmetric pairs pandas

I used to have the same problem before , And this is my solution df1=df[[‘X’,’Y’]].apply(sorted,1) df.groupby([df1.X,df1.Y])[‘count’].sum().reset_index(name=”count”) Out[400]: X Y count 0 A B 3 1 C D 8 solved Aggregate symmetric pairs pandas

[Solved] Compare values under multiple conditions of one column in Python

Try: #Use pd.Categorical to ensure sorting if column is not lexicographical ordered. df[‘type’] = pd.Categorical(df[‘type’], ordered=True, categories=[‘s1′,’s2′,’s3’]) df[‘result’] = df.sort_values(‘type’).groupby(‘name’)[‘value’].diff(-1) df[‘result’] = df[‘result’].lt(0).mask(df[‘result’].isna(),”) df Output: index name type value result 0 1 A s1 20 False 1 2 A s2 10 2 3 B s1 18 True 3 4 B s2 32 False 4 5 … Read more

[Solved] Another Traceback Error When I Run My Python Code

You just have to many brackets ((df[‘Location’].str.contains(‘- Display’) & df[‘Lancaster’] == ” & df[‘Dakota’] == ‘D’ & df[‘Spitfire’] == ‘SS’ & df[‘Hurricane’] == ”)) You needed to remove a ‘)’ after each (‘- Display’) it looks like you will still have some problems with sorting through your data. But this should get you past your … Read more

[Solved] Customize axes in Matplotlib

You can display subscripts by writing your column names using LaTex: import pandas as pd import matplotlib.pyplot as plt df = pd.DataFrame( { 0: { “Method 1”: 31.7, “Method 2”: 44.2, “Method 3”: 75.6, “Method 4”: 87.5, “Method 5”: 88.6, “Method 6”: 100.0, }, 1: { “Method 1”: 32.9, “Method 2”: 45.4, “Method 3”: 72.2, … Read more

[Solved] Pandas: get json from data frame

You can use: #convert int xolum to string df[‘member_id’] = df.member_id.astype(str) #reshaping and convert to months period df.set_index(‘member_id’, inplace=True) df = df.unstack().reset_index(name=”val”).rename(columns={‘level_0′:’date’}) df[‘date’] = pd.to_datetime(df.date).dt.to_period(‘m’).dt.strftime(‘%Y-%m’) #groupby by date and member_id and aggregate sum df = df.groupby([‘date’,’member_id’])[‘val’].sum() #convert all values !=0 to 1 df = (df != 0).astype(int).reset_index() #working in pandas 0.18.1 d = df.groupby(‘member_id’)[‘date’, ‘val’].apply(lambda … Read more

[Solved] Using yield in nested loop

As you’ve been told in the comments, I also don’t think you can save memory using yield in this case. However, if you only want to know how to use yield, this is one of the options: import pandas as pd data = [{ “id”: 123, “sports”: { “football”: { “amount”: 3, “count”: 54 }, … Read more

[Solved] How to separate the contents of parentheses and make a new dataframe column? [closed]

Seems like str.extract would work assuming the seat number is the numeric characters before 席 and the seat arrangement is the values inside the parenthesis: import numpy as np import pandas as pd df = pd.DataFrame({ ‘seat’: [’45席(1階カウンター4席、6〜8人テーブル1席2階地下それぞれ最大20人)’, np.nan, np.nan, np.nan, ‘9席(カウンター9席、個室4席)’] }) new_df = df[‘seat’].str.extract(r'(\d+)席((.*))’, expand=True) new_df.columns = [‘seat number’, ‘seat arrangement’] new_df: seat … Read more

[Solved] FREQUENCY BAR CHART OF A DATE COLUMN IN AN ASCENDING ORDER OF DATES

import pandas as pd import matplotlib.pyplot as plt data = pd.read_csv(‘dataset.csv’) data[‘sample_date’] = pd.to_datetime(data[‘sample_date’]) data[‘sample_date’].value_counts().sort_index().plot(kind=’bar’) # Use sort_index() plt.tight_layout() plt.show() 0 solved FREQUENCY BAR CHART OF A DATE COLUMN IN AN ASCENDING ORDER OF DATES

[Solved] python – how do i assign columns to my dataframe?

import pandas as pd varnames = [‘Student_id’,’First_Name’,’Last_Name’,’Grade’] values = [[‘156841′,’Mark’,’Smith’,’85’], [‘785496′,’Jason’,’Gross’,’90’], [‘785612′,’Laura’,’Clarkson’,’76’], [‘125465′,’Tria’,’Carr’,’100′]] data1 = pd.DataFrame(values, columns=varnames) data1 solved python – how do i assign columns to my dataframe?