Convert Python list to pandas Series
I understand that your list is in fact a list of lists import pandas as pd thelist = [ [‘sentence 1’], [‘sentence 2’], [‘sentence 3’] ] df = pd.Series( (v[0] for v in thelist) )
I understand that your list is in fact a list of lists import pandas as pd thelist = [ [‘sentence 1’], [‘sentence 2’], [‘sentence 3’] ] df = pd.Series( (v[0] for v in thelist) )
You can try Series.rename: df = pd.concat((df, s.rename(‘col’)), axis=1)
If you only need to get list of unique values, you can just use unique method. If you want to have Python’s set, then do set(some_series) In [1]: s = pd.Series([1, 2, 3, 1, 1, 4]) In [2]: s.unique() Out[2]: array([1, 2, 3, 4]) In [3]: set(s) Out[3]: {1, 2, 3, 4} However, if you … Read more
I believe the functionality you’re looking for is in the hist method of a Series object which wraps the hist() function in matplotlib Here’s the relevant documentation In [10]: import matplotlib.pyplot as plt In [11]: plt.hist? … Plot a histogram. Compute and draw the histogram of *x*. The return value is a tuple (*n*, *bins*, … Read more
Maybe an easier way would be to add the pandas.Series into the pandas.DataFrame with ignore_index=True argument to DataFrame.append(). Example – DF = DataFrame() for sample,data in D_sample_data.items(): SR_row = pd.Series(data.D_key_value) DF = DF.append(SR_row,ignore_index=True) Demo – In [1]: import pandas as pd In [2]: df = pd.DataFrame([[1,2],[3,4]],columns=[‘A’,’B’]) In [3]: df Out[3]: A B 0 1 2 … Read more
method 1 sample.housing.eq(‘yes’).mul(1) method 2 pd.Series(np.where(sample.housing.values == ‘yes’, 1, 0), sample.index) method 3 sample.housing.map(dict(yes=1, no=0)) method 4 pd.Series(map(lambda x: dict(yes=1, no=0)[x], sample.housing.values.tolist()), sample.index) method 5 pd.Series(np.searchsorted([‘no’, ‘yes’], sample.housing.values), sample.index) All yield 0 0 1 0 2 1 3 0 4 0 5 0 6 0 7 0 8 1 9 1 timing given sample timing … Read more
Using Boolean Indexing >>> s = pd.Series([True, False, True, True, False, False, False, True]) >>> s[s].index Int64Index([0, 2, 3, 7], dtype=”int64″) If need a np.array object, get the .values >>> s[s].index.values array([0, 2, 3, 7]) Using np.nonzero >>> np.nonzero(s) (array([0, 2, 3, 7]),) Using np.flatnonzero >>> np.flatnonzero(s) array([0, 2, 3, 7]) Using np.where >>> np.where(s)[0] … Read more
Use shift. df[‘dA’] = df[‘A’] – df[‘A’].shift(-1)
Place both series in Python’s set container then use the set intersection method: s1.intersection(s2) and then transform back to list if needed. Just noticed pandas in the tag. Can translate back to that: pd.Series(list(set(s1).intersection(set(s2)))) From comments I have changed this to a more Pythonic expression, which is shorter and easier to read: Series(list(set(s1) & set(s2))) … Read more
>>> s = pd.Series([1,2,3,4,np.NaN,5,np.NaN]) >>> s[~s.isnull()] 0 1 1 2 2 3 3 4 5 5 update or even better approach as @DSM suggested in comments, using pandas.Series.dropna(): >>> s.dropna() 0 1 1 2 2 3 3 4 5 5