C RUBY-ON-RAILS MYSQL ASP.NET DEVELOPMENT RUBY .NET LINUX SQL-SERVER REGEX WINDOWS ALGORITHM ECLIPSE VISUAL-STUDIO STRING SVN PERFORMANCE APACHE-FLEX UNIT-TESTING SECURITY LINQ UNIX MATH EMAIL OOP LANGUAGE-AGNOSTIC VB6 MSBUILD

# Calculate pandas DataFrame column by custom routine which accepts dictionary as input

By : Direshan
Date : November 22 2020, 03:01 PM
wish helps you There is pandas DataFrame with numeric columns A1 and A2. , Step 1:
code :
``````import pandas as pd
import numpy as np

df = pd.DataFrame(np.random.rand(5,2))

dicts = []
for row in df.itertuples():
dicts.append({df.columns[0]: row._1, df.columns[1]: row._2})
``````
``````packed_blobs = [pack(x) for x in dicts]
``````
``````dt = np.dtype(('column_name', 'blob_format'))
x = np.array(packed_blobs, dtype=dt)
df['new_col'] = x
``````

Share :

## How can I calculate values in a Pandas dataframe based on another column in the same dataframe

By : Zhipeng Zhu
Date : March 29 2020, 07:55 AM
will be helpful for those in need For anyone wondering, the problem was that span could not take multiple values, which was happening when I tried to pass df['ideal_moving_average'] into it. Instead, I used the below code, which seemed to go line by line passing the value for that row into span.
code :
``````df['30ema'] = df['Adj Close'].ewm(span=df.iloc[-1]['ideal_ma'], min_periods=0, ignore_na=True).mean()
``````

## Create New Dictionary from Old Dictionary Pandas DataFrame to calculate entropy

By : Kate
Date : March 29 2020, 07:55 AM
help you fix your problem It's not clear how entropy calculation fits into you specified input and output, but here's one way to get the output you want, using a mix of Pandas and basic Python.
code :
``````import pandas as pd

data = {1: ["'stop'", "'avoid'", "'stifle'", "'not'", "'squelch'",
"'contain'", "'cover'", "'suppress'"],
2: ["'hold'"],
3: ["'burke'"],
4: ["'hod'"]}
s = pd.Series(data)

s
1    ['stop', 'avoid', 'stifle', 'not', 'squelch', ...
2                                             ['hold']
3                                            ['burke']
4                                              ['hod']
dtype: object
``````
``````s2 = s.apply(lambda x: (x[0]+" ")*len(x))

s2
1    'stop' 'stop' 'stop' 'stop' 'stop' 'stop' 'sto...
2                                              'hold'
3                                             'burke'
4                                               'hod'
dtype: object
``````
``````slist = []
for valset in s2:
# strip the trailing space in each valset
for val in valset.strip().split(" "):
slist.extend([val])

slist
["'stop'", "'stop'", "'stop'",  "'stop'", "'stop'",  "'stop'",
"'stop'", "'stop'",  "'hold'",  "'burke'", "'hod'"]
``````

## Using dictionary as a reference to calculate number a new column in a pandas dataframe from a different dataframe

By : R32
Date : March 29 2020, 07:55 AM
hop of those help? First we using for loop with isin lookup the dict create the value we need
code :
``````l=[(df2.loc[df2.UID.isin(d[x]),'Job'].sum(),df2.loc[df2.UID.isin(d[x]),'Vaccinated'].sum()) for x in df1.UID]
#here we create the new df to concat
newdf=pd.DataFrame(l,columns=['nFriends_Jobs','nFriends_Vacc '],index=df1.index)
df1=pd.concat([df1,newdf],1)
df1
Out[187]:
UID Sex  Infected  nFriends_Jobs  nFriends_Vacc
0  111   M      True              2               1
1  112   F      True              1               1
2  113   F     False              1               1
3  114   M     False              2               1
4  115   F     False              2               2
``````

## Custom Column Selection in Pandas DataFrame.Groupby.Agg's dictionary

By : Newmark
Date : March 29 2020, 07:55 AM
should help you out 1) To determine if a column is numeric, you can use pandas.api.types.is_numeric_dtype
2) To find the remaining columns, you can use set(df.columns) minus the columns you used in groupby and those with specific agg functions, for example
code :
``````from pandas.api.types import is_numeric_dtype

fields_groupby = ['Day', 'Month']
fields_specific = {
'High': [min, 'mean', max],
'Low': [min, 'mean', max],
'Open': 'mean',
'Size': lambda x: x.value_counts().index[0],
}
fields_other = set(set(stock.columns) - set(fields_groupby) - set(fields_specific))
fields_agg_remaining = {col: 'mean' if is_numeric_dtype(stock[col]) else lambda x: x.value_counts().index[1] for col in fields_other}
``````
``````agg_fields = fields_agg_remaining
agg_fields.update(fields_specific)
stock.groupby(['Day', 'Month']).agg(agg_fields).round(2)
``````
``````stock.groupby(['Day', 'Month']).agg(
{col:
[min, 'mean', max] if col in ['High', 'Low'] else
'mean' if col in ['Open'] else
lambda x: x.value_counts().index[0] if col in ['Size'] else
'mean' if is_numeric_dtype(stock[col]) else
lambda x: x.value_counts().index[1] for col in set(set(stock.columns) - {'Day', 'Month'})}
).round(2)
``````

## Overriding a pandas DataFrame column with dictionary values, where the dictionary keys match a non-index column?

By : Nikolay Solakov
Date : March 29 2020, 07:55 AM
wish helps you Assuming it would be OK to propagate the new values to all rows where column a matches (in the event there were duplicates in column a) then: