Seaborn groupby pandas Series - matplotlib
I want to visualize my data into box plots that are grouped by another variable shown here in my terrible drawing:
So what I do is to use a pandas series variable to tell pandas that I have grouped variables so this is what I do:
import pandas as pd
import seaborn as sns
#example data for reproduciblity
a = pd.DataFrame(
[
[2, 1],
[4, 2],
[5, 1],
[10, 2],
[9, 2],
[3, 1]
])
#converting second column to Series
a.ix[:,1] = pd.Series(a.ix[:,1])
#Plotting by seaborn
sns.boxplot(a, groupby=a.ix[:,1])
And this is what I get:
However, what I would have expected to get was to have two boxplots each describing only the first column, grouped by their corresponding column in the second column (the column converted to Series), while the above plot shows each column separately which is not what I want.
A column in a Dataframe is already a Series, so your conversion is not necessary. Furthermore, if you only want to use the first column for both boxplots, you should only pass that to Seaborn.
So:
#example data for reproduciblity
df = pd.DataFrame(
[
[2, 1],
[4, 2],
[5, 1],
[10, 2],
[9, 2],
[3, 1]
], columns=['a', 'b'])
#Plotting by seaborn
sns.boxplot(df.a, groupby=df.b)
I changed your example a little bit, giving columns a label makes it a bit more clear in my opinion.
edit:
If you want to plot all columns separately you (i think) basically want all combinations of the values in your groupby column and any other column. So if you Dataframe looks like this:
a b grouper
0 2 5 1
1 4 9 2
2 5 3 1
3 10 6 2
4 9 7 2
5 3 11 1
And you want boxplots for columns a and b while grouped by the column grouper. You should flatten the columns and change the groupby column to contain values like a1, a2, b1 etc.
Here is a crude way which i think should work, given the Dataframe shown above:
dfpiv = df.pivot(index=df.index, columns='grouper')
cols_flat = [dfpiv.columns.levels[0][i] + str(dfpiv.columns.levels[1][j]) for i, j in zip(dfpiv.columns.labels[0], dfpiv.columns.labels[1])]
dfpiv.columns = cols_flat
dfpiv = dfpiv.stack(0)
sns.boxplot(dfpiv, groupby=dfpiv.index.get_level_values(1))
Perhaps there are more fancy ways of restructuring the Dataframe. Especially the flattening of the hierarchy after pivoting is hard to read, i dont like it.
This is a new answer for an old question because in seaborn and pandas are some changes through version updates. Because of this changes the answer of Rutger is not working anymore.
The most important changes are from seaborn==v0.5.x to seaborn==v0.6.0. I quote the log:
Changes to boxplot() and violinplot() will probably be the most disruptive. Both functions maintain backwards-compatibility in terms of the kind of data they can accept, but the syntax has changed to be more similar to other seaborn functions. These functions are now invoked with x and/or y parameters that are either vectors of data or names of variables in a long-form DataFrame passed to the new data parameter.
Let's now go through the examples:
# preamble
import pandas as pd # version 1.1.4
import seaborn as sns # version 0.11.0
sns.set_theme()
Example 1: Simple Boxplot
df = pd.DataFrame([[2, 1] ,[4, 2],[5, 1],
[10, 2],[9, 2],[3, 1]
], columns=['a', 'b'])
#Plotting by seaborn with x and y as parameter
sns.boxplot(x='b', y='a', data=df)
Example 2: Boxplot with grouper
df = pd.DataFrame([[2, 5, 1], [4, 9, 2],[5, 3, 1],
[10, 6, 2],[9, 7, 2],[3, 11, 1]
], columns=['a', 'b', 'grouper'])
# usinge pandas melt
df_long = pd.melt(df, "grouper", var_name='a', value_name='b')
# join two columns together
df_long['a'] = df_long['a'].astype(str) + df_long['grouper'].astype(str)
sns.boxplot(x='a', y='b', data=df_long)
Example 3: rearanging the DataFrame to pass is directly to seaborn
def df_rename_by_group(data:pd.DataFrame, col:str)->pd.DataFrame:
'''This function takes a DataFrame, groups by one column and returns
a new DataFrame where the old columnnames are extended by the group item.
'''
grouper = df.groupby(col)
max_length_of_group = max([len(values) for item, values in grouper.indices.items()])
_df = pd.DataFrame(index=range(max_length_of_group))
for i in grouper.groups.keys():
helper = grouper.get_group(i).drop(col, axis=1).add_suffix(str(i))
helper.reset_index(drop=True, inplace=True)
_df = _df.join(helper)
return _df
df = pd.DataFrame([[2, 5, 1], [4, 9, 2],[5, 3, 1],
[10, 6, 2],[9, 7, 2],[3, 11, 1]
], columns=['a', 'b', 'grouper'])
df_new = df_rename_by_group(data=df, col='grouper')
sns.boxplot(data=df_new)
I really hope this answer helps to avoid some confusion.
sns.boxplot() doesnot take groupby.
Probably you are gonna see
TypeError: boxplot() got an unexpected keyword argument 'groupby'.
The best idea to group data and use in boxplot passing the data as groupby dataframe value.
import seaborn as sns
grouDataFrame = nameDataFrame(['A'])['B'].agg(sum).reset_index()
sns.boxplot(y='B', x='A', data=grouDataFrame)
Here B column data contains numeric value and grouped is done on the basis of A. All the grouped value with their respective column are added and boxplot diagram is plotted. Hope this helps.
Related
i need to return a value from a dataframe cell as a variable not a series
i have the following issue: when i use .loc funtion it returns a series not a single value with no index. As i need to do some math operation with the selected cells. the function that i am using is: import pandas as pd data = [[82,1], [30, 2], [3.7, 3]] df = pd.DataFrame(data, columns = ['Ah-Step', 'State']) df['Ah-Step'].loc[df['State']==2]+ df['Ah-Step'].loc[df['State']==3]
.values[0] will do what OP wants. Assuming one wants to obtain the value 30, the following will do the work df.loc[df['State'] == 2, 'Ah-Step'].values[0] print(df) [Out]: 30.0 So, in OP's specific case, the operation 30+3.7 could be done as follows df.loc[df['State'] == 2, 'Ah-Step'].values[0] + df['Ah-Step'].loc[df['State']==3].values[0] [Out]: 33.7
Pandas: how to retrieve values from a DataFrame given a list of (row, column) pairs?
tldr; I want to pass a series of positions on a DataFrame and receive a series of values, If possible with a DataFrame method. I have a Dataframe with some columns and an index import pandas as pd df_a = pd.DataFrame( {'A':[0,1,3,7], 'B':[2,3,4,5]}, index=[0,1,2,3]) I want to retrieve the values at specific (row, column) positions on the DataFrame rows = [0, 2, 3] cols = ['A','B','A'] df_a.loc[rows, cols] returns a 3x3 DataFrame |A |B |A 0 0 2 0 2 3 4 3 3 7 5 7 I want the series of values corresponding to the (row, col) values, a series of length 3 [0, 4, 7] What is the best way to do this in pandas?
Most certainly! you can use DataFrame.lookup to achieve exactly what you want: https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.lookup.html import pandas as pd df_a = pd.DataFrame({'A':[0,1,3,7], 'B':[2,3,4,5]}, index=[0,1,2,3]) rows = [0, 2, 3] cols = ['A','B','A'] values = df_a.lookup(rows, cols) print(values) array([0, 4, 7], dtype=int64)
Pandas does not support that kind of indexing, only numpy >>> df.to_numpy()[rows, df.columns.get_indexer(cols)] array([0, 4, 7])
Assert an integer is in list on pandas series
I have a DataFrame with two pandas Series as follow: value accepted_values 0 1 [1, 2, 3, 4] 1 2 [5, 6, 7, 8] I would like to efficiently check if the value is in accepted_values using pandas methods. I already know I can do something like the following, but I'm interested in a faster approach if there is one (took around 27 seconds on 1 million rows DataFrame) import pandas as pd df = pd.DataFrame({"value":[1, 2], "accepted_values": [[1,2,3,4], [5, 6, 7, 8]]}) def check_first_in_second(values: pd.Series): return values[0] in values[1] are_in_accepted_values = df[["value", "accepted_values"]].apply( check_first_in_second, axis=1 ) if not are_in_accepted_values.all(): raise AssertionError("Not all value in accepted_values")
I think if create DataFrame with list column you can compare by DataFrame.eq and test if match at least one value per row by DataFrame.any: df1 = pd.DataFrame(df["accepted_values"].tolist(), index=df.index) are_in_accepted_values = df1.eq(df["value"]).any(axis=1).all() Another idea: are_in_accepted_values = all(v in a for v, a in df[["value", "accepted_values"]].to_numpy())
I found a little optimisation to your second idea. Using a bit more numpy than pandas makes it faster (more than 3x, tested with time.perf_counter()). values = df["value"].values accepted_values = df["accepted_values"].values are_in_accepted_values = all(s in e for s, e in np.column_stack([values, accepted_values]))
Apply function to list of columns from a dataframe
I'm creating a function that accepts 3 inputs: a dataframe, a column and a list of columns. The function should apply a short calculation to the single column, and a different short calculation to the list of other columns. It should return a dataframe containing just the amended columns (and their amended rows) from the original dataframe. import numpy as np df = pd.DataFrame([[1, 2, 3, 4], [1, 3, 5, 6], [4, 6, 7, 8], [5, 4, 3, 6], columns=['A', 'B', 'C', 'D']) def pre_process(dataframe, y_col_name, x_col_names): return = new_dataframe The calculation to be applied to y_col_name's rows is each value of y_col_name divided by the mean of y_col_name. The calculation to be applied to each of the list of columns in x_col_name is each value of each column, divided by the column's standard deviation. I would like some help to write the function. I think I need to use an "apply" or a "lambda" function but I'm unsure. This is what calling the command would look like: pre_process_data = preprocess(df,'A', ['B','D']) Thanks
def pre_process(dataframe, y_col_name, x_col_names): new_dataframe = dataframe.copy() new_dataframe[y_col_name] = new_dataframe[y_col_name]/new_dataframe[y_col_name].mean() new_dataframe[x_col_names] = new_dataframe[x_col_names]/new_dataframe[x_col_names].std() return new_dataframe Is this what you mean?
Get column-index from column-name in pandas? [duplicate]
In R when you need to retrieve a column index based on the name of the column you could do idx <- which(names(my_data)==my_colum_name) Is there a way to do the same with pandas dataframes?
Sure, you can use .get_loc(): In [45]: df = DataFrame({"pear": [1,2,3], "apple": [2,3,4], "orange": [3,4,5]}) In [46]: df.columns Out[46]: Index([apple, orange, pear], dtype=object) In [47]: df.columns.get_loc("pear") Out[47]: 2 although to be honest I don't often need this myself. Usually access by name does what I want it to (df["pear"], df[["apple", "orange"]], or maybe df.columns.isin(["orange", "pear"])), although I can definitely see cases where you'd want the index number.
Here is a solution through list comprehension. cols is the list of columns to get index for: [df.columns.get_loc(c) for c in cols if c in df]
DSM's solution works, but if you wanted a direct equivalent to which you could do (df.columns == name).nonzero()
For returning multiple column indices, I recommend using the pandas.Index method get_indexer, if you have unique labels: df = pd.DataFrame({"pear": [1, 2, 3], "apple": [2, 3, 4], "orange": [3, 4, 5]}) df.columns.get_indexer(['pear', 'apple']) # Out: array([0, 1], dtype=int64) If you have non-unique labels in the index (columns only support unique labels) get_indexer_for. It takes the same args as get_indexer: df = pd.DataFrame( {"pear": [1, 2, 3], "apple": [2, 3, 4], "orange": [3, 4, 5]}, index=[0, 1, 1]) df.index.get_indexer_for([0, 1]) # Out: array([0, 1, 2], dtype=int64) Both methods also support non-exact indexing with, f.i. for float values taking the nearest value with a tolerance. If two indices have the same distance to the specified label or are duplicates, the index with the larger index value is selected: df = pd.DataFrame( {"pear": [1, 2, 3], "apple": [2, 3, 4], "orange": [3, 4, 5]}, index=[0, .9, 1.1]) df.index.get_indexer([0, 1]) # array([ 0, -1], dtype=int64)
When you might be looking to find multiple column matches, a vectorized solution using searchsorted method could be used. Thus, with df as the dataframe and query_cols as the column names to be searched for, an implementation would be - def column_index(df, query_cols): cols = df.columns.values sidx = np.argsort(cols) return sidx[np.searchsorted(cols,query_cols,sorter=sidx)] Sample run - In [162]: df Out[162]: apple banana pear orange peach 0 8 3 4 4 2 1 4 4 3 0 1 2 1 2 6 8 1 In [163]: column_index(df, ['peach', 'banana', 'apple']) Out[163]: array([4, 1, 0])
Update: "Deprecated since version 0.25.0: Use np.asarray(..) or DataFrame.values() instead." pandas docs In case you want the column name from the column location (the other way around to the OP question), you can use: >>> df.columns.values()[location] Using #DSM Example: >>> df = DataFrame({"pear": [1,2,3], "apple": [2,3,4], "orange": [3,4,5]}) >>> df.columns Index(['apple', 'orange', 'pear'], dtype='object') >>> df.columns.values()[1] 'orange' Other ways: df.iloc[:,1].name df.columns[location] #(thanks to #roobie-nuby for pointing that out in comments.)
To modify DSM's answer a bit, get_loc has some weird properties depending on the type of index in the current version of Pandas (1.1.5) so depending on your Index type you might get back an index, a mask, or a slice. This is somewhat frustrating for me because I don't want to modify the entire columns just to extract one variable's index. Much simpler is to avoid the function altogether: list(df.columns).index('pear') Very straightforward and probably fairly quick.
how about this: df = DataFrame({"pear": [1,2,3], "apple": [2,3,4], "orange": [3,4,5]}) out = np.argwhere(df.columns.isin(['apple', 'orange'])).ravel() print(out) [1 2]
When the column might or might not exist, then the following (variant from above works. ix = 'none' try: ix = list(df.columns).index('Col_X') except ValueError as e: ix = None pass if ix is None: # do something
import random def char_range(c1, c2): # question 7001144 for c in range(ord(c1), ord(c2)+1): yield chr(c) df = pd.DataFrame() for c in char_range('a', 'z'): df[f'{c}'] = random.sample(range(10), 3) # Random Data rearranged = random.sample(range(26), 26) # Random Order df = df.iloc[:, rearranged] print(df.iloc[:,:15]) # 15 Col View for col in df.columns: # List of indices and columns print(str(df.columns.get_loc(col)) + '\t' + col) ![Results](Results