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
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
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?
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
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)
|