series

How to convert Dataframe into Series?

二次信任 提交于 2019-12-01 07:33:20
I want to convert N columns into one series. How to do it effectively? Input: 0 1 2 3 0 64 98 47 58 1 80 94 81 46 2 18 43 79 84 3 57 35 81 31 Expected Output: 0 64 1 80 2 18 3 57 4 98 5 94 6 43 7 35 8 47 9 81 10 79 11 81 12 58 13 46 14 84 15 31 dtype: int64 So Far I tried: print df[0].append(df[1]).append(df[2]).append(df[3]).reset_index(drop=True) I'm not satisfied with my solution, moreover it won't work for dynamic columns. Please help me to find a better approach. You can also use Series class and .values attribute: pd.Series(df.values.T.flatten()) Output: 0 64 1 80 2 18 3 57 4 98 5 94 6

How to apply a function on a Series

六月ゝ 毕业季﹏ 提交于 2019-12-01 07:12:40
Given a Series s : Name 0 Tennessee Oilers 1 Tennessee Titans 2 Washington Redskins I would like a apply a function to rename the values. translate = { 'Houston Oilers': 'Tennessee Titans', 'Tennessee Oilers': 'Tennessee Titans' } s = s.apply(lambda x: translate.get(x, x)) And this raises: TypeError: ("'Series' objects are mutable, thus they cannot be hashed", u'occurred at index 0') Had I applied this on DataFrame's column instead, this would have worked. I thought I was doing this according to the docs Can you correct me please? Use map to perform the lookup: In [204]: translate = { 'Houston

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

断了今生、忘了曾经 提交于 2019-12-01 06:36:10
I have a huge dataframe, and I index it like so: df.ix[<integer>] Depending on the index, sometimes this will have only one row of values. Pandas automatically converts this to a Series, which, quite frankly, is annoying because I can't operate on it the same way I can a df. How do I either: 1) Stop pandas from converting and keep it as a dataframe ? OR 2) easily convert the resulting series back to a dataframe ? pd.DataFrame(df.ix[<integer>]) does not work because it doesn't keep the original columns. It treats the <integer> as the column, and the columns as indices. Much appreciated. You can

How to get next point in Highcharts tooltip

你说的曾经没有我的故事 提交于 2019-12-01 06:16:28
问题 how can I access to the next point of the series from the tooltip formatter. Because I want to do a sum between both points. Like this.y + next.y. But I don't know how to have an access to the next point. If someone have an answer. Thanks 回答1: This need to be done in a few steps: get x-index according to x-value: var index = this.series.xData.indexOf(this.x); now get y-value: var nextY = this.series.yData[index+1]; And all you need to do is to sum values, like this: var sum = this.y + nextY;

How to change series name in VBA

本小妞迷上赌 提交于 2019-12-01 04:42:54
问题 I have a series of charts I am creating using VBA (code below). I am having trouble changing the names of the series from series 1 and series 2 to Current State and Solution State. I keep getting an Object Variable or With Block Variable not set error. However without the srs1 and srs2 code the charts work just fine (just with the wrong series names). I looked up how to fix this and the answer I received however is not working for me. Does anyone know another way to do this? Sub MA() Dim Srs1

Assign values to multiple columns in Pandas

一个人想着一个人 提交于 2019-12-01 04:18:26
I have follow simple DataFrame - df : 0 0 1 1 2 2 3 Once I try to create a new columns and assign some values for them, as example below: df['col2', 'col3'] = [(2,3), (2,3), (2,3)] I got following structure 0 (col2, col3) 0 1 (2, 3) 1 2 (2, 3) 2 3 (2, 3) However, I am looking a way to get as here: 0 col2, col3 0 1 2, 3 1 2 2, 3 2 3 2, 3 Looks like solution is simple: df['col2'], df['col3'] = zip(*[(2,3), (2,3), (2,3)]) There is a convenient solution to joining multiple series to a dataframe via a list of tuples. You can construct a dataframe from your list of tuples before assignment: df = pd

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

余生长醉 提交于 2019-12-01 04:05:10
问题 I have a huge dataframe, and I index it like so: df.ix[<integer>] Depending on the index, sometimes this will have only one row of values. Pandas automatically converts this to a Series, which, quite frankly, is annoying because I can't operate on it the same way I can a df. How do I either: 1) Stop pandas from converting and keep it as a dataframe ? OR 2) easily convert the resulting series back to a dataframe ? pd.DataFrame(df.ix[<integer>]) does not work because it doesn't keep the

Evaluating pandas series values with logical expressions and if-statements

▼魔方 西西 提交于 2019-12-01 02:44:48
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 following error message: Traceback (most recent call last): File "<pyshell#69>", line 1, in <module> if pnl

What happens when you compare 2 pandas Series

馋奶兔 提交于 2019-12-01 02:08:53
I ran up against unexpected behavior in pandas when comparing two series. I wanted to know if this is intended or a bug. suppose I: import pandas as pd x = pd.Series([1, 1, 1, 0, 0, 0], index=['a', 'b', 'c', 'd', 'e', 'f'], name='Value') y = pd.Series([0, 2, 0, 2, 0, 2], index=['c', 'f', 'a', 'e', 'b', 'd'], name='Value') x > y yields: a True b False c True d False e False f False Name: Value, dtype: bool which isn't what I wanted. Clearly, I expected the indexes to line up. But I have to explicitly line them up to get the desired results. x > y.reindex_like(x) yields: a True b True c True d

AttributeError: 'Series' object has no attribute 'searchsorted' pandas

冷暖自知 提交于 2019-11-30 23:54:39
问题 I reproduce the code of book python for data analysis in page 38 I write prop_cumsum = df.sort_index(by='prop', ascending=False).prop.cumsum() and prop_cumsum.searchsorted(0.5) Then there is an error say: AttributeError Traceback (most recent call last) <ipython-input-30-f2e2bb3f5ba0> in <module>() ----> 1 prop_cumsum.searchsorted(0.5) C:\Users\xxx\AppData\Local\Enthought\Canopy32\User\lib\site-packages\pandas\core\generic.pyc in __getattr__(self, name) 1813 return self[name] 1814 raise