logo
down
shadow

Why does a pandas Series of DataFrame mean fail, but sum does not, and how to make it work


Why does a pandas Series of DataFrame mean fail, but sum does not, and how to make it work

By : Bob Miller
Date : November 21 2020, 07:31 AM
around this issue There may be a smarter way to do this in Python Pandas, but the following example should, but doesn't work: , When you define s with
code :
s = pd.Series([df1, df2, df3], index=idx)
In [77]: s
Out[77]: 
2010-01-01 00:00:00       a  b
0  1  0
1  1  2
2  2  0
2010-01-01 01:00:00       a  b
0  1  0
1  1  2
2  2  0
2010-01-01 02:00:00       a  b
0  1  0
1  1  2
2  2  0
Freq: H, dtype: object
In [78]: s.sum()
Out[78]: 
   a  b
0  3  0
1  3  6
2  6  0
def nanmean(values, axis=None, skipna=True):
    values, mask, dtype, dtype_max = _get_values(values, skipna, 0)
    the_sum = _ensure_numeric(values.sum(axis, dtype=dtype_max))
    ...
In [79]: s = pd.concat([df1, df2, df3], keys=idx)

In [80]: s
Out[80]: 
                       a  b
2010-01-01 00:00:00 0  1  0
                    1  1  2
                    2  2  0
2010-01-01 01:00:00 0  1  0
                    1  1  2
                    2  2  0
2010-01-01 02:00:00 0  1  0
                    1  1  2
                    2  2  0
In [82]: s.sum(level=1)
Out[82]: 
   a  b
0  3  0
1  3  6
2  6  0

In [84]: s.mean(level=1)
Out[84]: 
   a  b
0  1  0
1  1  2
2  2  0


Share : facebook icon twitter icon
how to make 1 by n dataframe from series in pandas?

how to make 1 by n dataframe from series in pandas?


By : niky sharma
Date : March 29 2020, 07:55 AM
it fixes the issue I have a huge dataframe, and I index it like so: , You can do df.ix[[n]] to get a one-row dataframe of row n.
Python pandas column asignment between dataframe and series does not work

Python pandas column asignment between dataframe and series does not work


By : Mujeeb Qureshi
Date : November 26 2020, 11:01 PM
should help you out For me works creating new DataFrame df1 and then concat to original df:
code :
def func(x):
    return pd.Series({'d':{'foo':5, 'bar':10}, 'c':300})

df1 = df.apply(lambda x: func(x), axis=1)
print (df1)
     c                      d
0  300  {'bar': 10, 'foo': 5}
1  300  {'bar': 10, 'foo': 5}
2  300  {'bar': 10, 'foo': 5}

print (pd.concat([df[['a','b']], df1], axis=1))
   a    b    c                      d
0  1  100  300  {'bar': 10, 'foo': 5}
1  2  100  300  {'bar': 10, 'foo': 5}
2  3  100  300  {'bar': 10, 'foo': 5}
pandas get the value and the location from another DataFrame and make a series

pandas get the value and the location from another DataFrame and make a series


By : ronnee
Date : March 29 2020, 07:55 AM
should help you out Use DataFrame contructor with idxmin and min per rows:
code :
df1 = pd.DataFrame({'MinCol': dfrtn2.idxmin(axis=1), 
                   'Minimum': dfrtn2.min(axis=1)}, columns=['Minimum','MinCol'])
print (df1)
   Minimum MinCol
0        0      C
1        0      D
2        0      E
3        0      F
4        1      A
df1['new'] = dfrtn.lookup(dfrtn.index, df1['MinCol'])
print (df1)
   Minimum MinCol  new
0        0      C   35
1        0      D   42
2        0      E   49
3        0      F   56
4        1      A   57
df1['Minimum'] = dfrtn.lookup(dfrtn.index, df1['MinCol'])
print (df1)
   Minimum MinCol
0       35      C
1       42      D
2       49      E
3       56      F
4       57      A
what is the best way to merge pandas.Dataframe with pandas.Series based on df.columns and Series.index names?

what is the best way to merge pandas.Dataframe with pandas.Series based on df.columns and Series.index names?


By : Gambler
Date : March 29 2020, 07:55 AM
help you fix your problem Suppose that you create these series as outputs output_rms_1, output_rms_2, etc., than the series can be combined in one dataframe
code :
import pandas as pd
dfRms = pd.DataFrame([output_rms_1, output_rms_2, output_rms_3])
dfRms = dfRms.append(output_rms_10, ignore_index=True)
result = pd.merge(wfm, dfRms, on=['CFN', 'OPN'], how='left')
Pandas - Add values from series to dataframe column based on index of series matching some value in dataframe

Pandas - Add values from series to dataframe column based on index of series matching some value in dataframe


By : user3531465
Date : March 29 2020, 07:55 AM
To fix this issue Data , I would use the second solution you propose or better this:
code :
df['cost']=(df['mark_up_id'].map(pb['mark_up']) + df['cost']).fillna(df['cost'])
df.assign( Cost=(df['mark_up_id'].map(pb['mark_up']) + df['cost']).fillna(df['cost']) )
%%timeit
df['cost']=(df['mark_up_id'].map(pb['mark_up']) + df['cost']).fillna(df['cost'])
#945 µs ± 46 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
%%timeit
test = df.join(pb, on='mark_up_id', how='left')
test['cost'].update(test['cost'] + test['mark_up'])
test.drop('mark_up',axis=1,inplace=True)
#3.59 ms ± 137 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
%%timeit
df['cost'].update(df['mark_up_id'].map(pb['mark_up']) + df['cost'])
#985 µs ± 32.8 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
Related Posts Related Posts :
  • Remove commas in a string, surrounded by a comma and double quotes / Python
  • How to chain Django querysets preserving individual order
  • Comparison with Python
  • How to find backlinks in a website with python
  • Return new instance of subclass when using methods inherited from parent class in Python
  • Which function in django.contrib.auth creates the default model permissions?
  • Formatting text in tabular form with Python
  • How to determine the first day of a month in Python
  • Error while converting date to timestamp in python
  • Python string iterations
  • Is there any file number limitation when you select multiple files with wxFileDialog?
  • Errors with Matplotlib when making an executable with Py2exe (Python)
  • Django Haystack - Indexing single field
  • Go Pro Hero 3 - Streaming video over wifi
  • Appending a column in .csv with Python/Pandas
  • How to change my result directory in Robot framework using RIDE?
  • problem with using pandas to manipulate a big text file in python
  • python-magic module' object has no attribute 'open'
  • Where goes wrong for this High Pass Filter in Python?
  • Why inserting keys in order into a python dict is faster than doint it unordered
  • flann index saving in python
  • Create new instance of list or dictionary without class
  • How can I easily convert FORTRAN code to Python code (real code, not wrappers)
  • Address of lambda function in python
  • Python adding space between characters in string. Most efficient way
  • python http server, multiple simultaneous requests
  • Disguising username & password on distributed python scripts
  • Post GraphQL mutation with Python Requests
  • Why doesnt pandas create an excel file?
  • Rolling comparison between a value and a past window, with percentile/quantile
  • How to avoid repetitive code when defining a new type in python with signature verification
  • How to configure uWSGI in order to debug with pdb (--honour-stdin configuration issue)
  • In Python, how do you execute objects that are functions from a list?
  • Python- Variable Won't Subtract?
  • Processing Power In Python
  • Python 2.7.2 - Cannot import name _random or random from sys
  • Why doesn't the Python sorted function take keyword order instead of reverse?
  • Make a function redirect to other functions depending on a variable
  • get_absolute_url in django-categories
  • Monitoring non-Celery background task with New Relic in Python
  • Feature selection with LinearSVC
  • LSTM - Predicting the same constant values after a while
  • Test the length of elements in a list
  • Django: render radiobutton with 3 columns, cost column must change according to size & quantity selected
  • Python class attributes vs global variable
  • sys.stdout.writelines("hello") and sys.stdout.write("hello")
  • is ndarray faster than recarray access?
  • Python - search through directory trees, rename certain files
  • GAE: How to build a query where a string begins with a value
  • TypeError: __init__() takes at least 2 arguments (1 given)
  • Overriding and customizing "django.contrib.auth.views.login"
  • Django : Redirect to a particular page after login
  • Python search and copy files in directory
  • pretty printing numpy ndarrays using unicode characters
  • Frequent pattern mining in Python
  • How can I make a set of functions that can be used synchronously as well as asynchronously?
  • Convert one dice roll to two dice roll
  • count occourrence in a list
  • Writing an If condition to filter out the first word
  • to read file and compare column in python
  • shadow
    Privacy Policy - Terms - Contact Us © ourworld-yourmove.org