series

Apply a method from a list of methods to pandas dataframe

末鹿安然 提交于 2019-12-10 18:17:54
问题 this is my first question here so please be patient with me. My problem is as follows: Assume we have a pandas Dataframe and we want to apply dynamically some pd.Series methods to a set of columns of this Dataframe. Why the following example doesn't work? testframe=pd.DataFrame.from_dict({'col1': [1,2] ,'col2': [3,4] }) funcdict={'col1':[pd.Series.astype,str.replace],'col2':[pd.Series.astype,str.replace]} argdict= {'col1':[['str'],['1','A']],'col2':[['str'],['3','B']]} for col in testframe

Creating a dataframe in pandas by multiplying two series together

倾然丶 夕夏残阳落幕 提交于 2019-12-10 14:05:55
问题 Say I have two series in pandas, series A and series B. How do I create a dataframe in which all of those values are multiplied together, i.e. with series A down the left hand side and series B along the top. Basically the same concept as this, where series A would be the yellow on the left and series B the yellow along the top, and all the values in between would be filled in by multiplication: http://www.google.co.uk/imgres?imgurl=http://www.vaughns-1-pagers.com/computer/multiplication

Sort dataframe by string length

无人久伴 提交于 2019-12-10 13:35:56
问题 I want to sort by name length. There doesn't appear to be a key parameter for sort_values so I'm not sure how to accomplish this. Here is a test df: import pandas as pd df = pd.DataFrame({'name': ['Steve', 'Al', 'Markus', 'Greg'], 'score': [2, 4, 2, 3]}) 回答1: You can use reindex of index of Series created by len with sort_values: print (df.name.str.len()) 0 5 1 2 2 6 3 4 Name: name, dtype: int64 print (df.name.str.len().sort_values()) 1 2 3 4 0 5 2 6 Name: name, dtype: int64 s = df.name.str

Applying lambda function to a pandas rolling window series

五迷三道 提交于 2019-12-10 11:03:10
问题 I have a function which takes an array and a value, and returns a value. I would like to apply it to my Series s on a rolling basis, so the array is always the rolling window. Here's a minimal example of what I've tried (unsuccessfully), using np.random.choice in place of my real function. I find lots of examples for finding rolling means and other built-in functions, but can't get it to work for my arbitrary lambda function. s = pd.Series([1,2,3,4,5,6,7,8,9]) rolling_window = s.rolling(3)

Access value by location in sorted pandas series with integer index

只谈情不闲聊 提交于 2019-12-10 07:56:05
问题 I have a pandas Series with an integer index which I've sorted (by value), how I access values by position in this Series. For example: s_original = pd.Series({0: -0.000213, 1: 0.00031399999999999999, 2: -0.00024899999999999998, 3: -2.6999999999999999e-05, 4: 0.000122}) s_sorted = np.sort(s_original) In [3]: s_original Out[3]: 0 -0.000213 1 0.000314 2 -0.000249 3 -0.000027 4 0.000122 In [4]: s_sorted Out[4]: 2 -0.000249 0 -0.000213 3 -0.000027 4 0.000122 1 0.000314 In [5]: s_sorted[3] Out[5]:

Summation series using matlab

≡放荡痞女 提交于 2019-12-10 04:35:40
问题 When i write this in matlab syms x; f=x^3-cos(x); g=diff(f) it gives out put as g = 3*x^2+sin(x) Now I want to generate summation series as I google and found "symsum" command but it doesn't do my required task, when i write the following commands syms k symsum(k^2, 0, 10) symsum(1/k^2,1,Inf) it gives the out put as ans = 385 ans = pi^2/6 Can you guys guide me how can I genereate the series which produce output as so that when I give command diff(Sk); it should produce result as or something

Number of occurrence of pair of value in dataframe

喜欢而已 提交于 2019-12-09 17:25:29
问题 I have dataframe with following columns: Name, Surname, dateOfBirth, city, country I am interested to find what is most common combination of name and surname and how much it occurs as well. Would be nice also to see list of top 10 combinations. My idea for top one was: mostFreqComb= df.groupby(['Name','Surname'])['Name'].count().argmax() But I think it is not giving me correct answer. Help would be much appreciated ! Thanks, Neb 回答1: For performance implications of the below solutions, see

How to add multiple series on a Chart in Excel using C#

夙愿已清 提交于 2019-12-09 13:05:41
问题 I would like to add a chart like the following picture. This chart has 3 series (Black, Red, Blue). The following is a block of code that creates "one" series on a chart... Excel._Workbook oWorkbook = (Excel._Workbook)oSheet.Parent; Excel._Chart oChart = (Excel._Chart)oWorkbook.Charts.Add(oSheet, Type.Missing, Type.Missing, Type.Missing); // Y axis data Excel.Range oRange = oSheet.get_Range(yRange, Type.Missing); // Creates a chart oChart.ChartWizard(oRange, chartType, 2, Excel.XlRowCol

Pandas - check if ALL values are NaN in Series

无人久伴 提交于 2019-12-09 07:26:49
问题 I have a data series which looks like this: print mys id_L1 2 NaN 3 NaN 4 NaN 5 NaN 6 NaN 7 NaN 8 NaN I would like to check is all the values are NaN. My attempt: pd.isnull(mys).all() Output: True Is this the correct way to do it? 回答1: Yes, that's correct, but I think a more idiomatic way would be: mys.isnull().all() 回答2: This will check for all columns.. mys.isnull().values.all(axis=0) 来源: https://stackoverflow.com/questions/33147158/pandas-check-if-all-values-are-nan-in-series

Evaluating pandas series values with logical expressions and if-statements

依然范特西╮ 提交于 2019-12-09 02:29:57
问题 I'm having trouble evaluating values from a dictionary using if statements. Given the following dictionary, which I imported from a dataframe (in case it matters): >>> pnl[company] 29: Active Credit Date Debit Strike Type 0 1 0 2013-01-08 2.3265 21.15 Put 1 0 0 2012-11-26 40 80 Put 2 0 0 2012-11-26 400 80 Put I tried to evaluate the following statment to establish the value of the last value of Active : if pnl[company].tail(1)['Active']==1: print 'yay' However,I was confronted by the