logo
Tags down

shadow

how to dcast pandas dataframe and convert rows to columns


By : aka aka
Date : October 15 2020, 08:10 PM
will be helpful for those in need Use drop_duplicates and then seems solution should be simplied:
code :
#by one column
price = price.drop_duplicates('rsp')
#if necessary by multiple columns
#cols = ['code','prod','date_from', 'date_to', 'rsp']
#price = price.drop_duplicates(subset=cols) 

g = price.groupby(['code','prod','date_from', 'date_to']).cumcount()

df1 = (price.set_index(['code','prod','date_from','date_to', g])
            .unstack()
            .sort_index(level=1, axis=1))

df1.columns = [f'{i}_{j+1}' for i, j in df1.columns]
df1 = df1.reset_index()
print (df1)
   code prod   date_from     date_to  rsp_1 time_from_1 time_to_1  rsp_2  \
0   123   HS  2018-01-01  2018-01-02   65.0       06:00     05:59    NaN   
1   123   HS  2018-01-02  2018-01-03   64.0       06:00     05:59    NaN   
2   123   MS  2018-01-01  2018-01-02   75.0       06:00     05:59   76.0   
3   123   MS  2018-01-02  2018-01-03   73.0       06:00     05:59    NaN   

  time_from_2 time_to_2  
0         NaN       NaN  
1         NaN       NaN  
2       10:00     05:59  
3         NaN       NaN  


Share : facebook icon twitter icon

pandas dataframe convert columns as rows


By : Mahima Tyagi
Date : March 29 2020, 07:55 AM
help you fix your problem I am looking for a way to convert my dataframe columns as rows. Following is my example dataframe: , Another option is to use melt:
code :
longDf = pd.melt(df.reset_index(), id_vars=['index'], var_name=['Columns'])
longDf[pd.notnull(longDf.value)]

  index Columns value
0   A   Col1    xx
2   C   Col1    xx
3   D   Col1    xx
4   E   Col1    xx
6   B   Col2    yy
9   E   Col2    yy

How to convert pandas dataframe rows into columns, based on category?


By : Krishna K
Date : March 29 2020, 07:55 AM
Any of those help You can use groupby by columns which first create new index and last column. then need aggreagate some way - I use mean, then convert one column DataFrame to Series by DataFrame.squeeze (then is not necessary remove top level of Multiindex in columns) and reshape by unstack. Last add_suffix to column name:
code :
df = df.groupby(['id','module']).mean().squeeze().unstack().add_suffix('_count')
print (df)
module  general_count  payroll_count  tax_count
id                                             
2                53.0            5.0        NaN
5                 NaN            NaN       35.0
df = df.pivot(index='id', columns='module')
df.columns = ['_'.join((col[1], col[0])) for col in df.columns]
print (df)
    general_count  payroll_count  tax_count
id                                         
2            53.0            5.0        NaN
5             NaN            NaN       35.0

Convert columns into multiple rows in pandas dataframe


By : STEVEO
Date : March 29 2020, 07:55 AM
may help you . I have a Dataframe that looks something like this: , You can use melt method.
code :
df = pd.melt(d, id_vars=["Deal", "Year", "Financial_Data"], 
             value_name="Quarter").drop(['variable'],axis=1).sort_values('Quarter')
   Deal  Year  Financial_Data  Quarter
0     1  1991             120        1
3     1  1991             120        2
6     1  1991             120        3
1     2  1992              80        4
4     2  1992              80        5
7     2  1992              80        6
2     3  1993             100        7
5     3  1993             100        8
8     3  1993             100        9
id_vars_list = df.columns.tolist()
id_vars_list = id_vars_list[:2] + id_vars_list[-1]
df = pd.melt(d, id_vars=id_vars_list, 
             value_name="Quarter").drop(['variable'],axis=1).sort_values('Quarter')

Convert Pandas DataFrame columns to rows


By : user1164630
Date : March 29 2020, 07:55 AM
Any of those help I have the following dict which I converted to dataframe , You need:
code :
df = df.stack().reset_index()
df.columns=['Player', 'Team', 'Score']
      Player    Team    Score
0   AD Hales    Score   1340.0
1   AJ Finch    Team    1082.0
2   Asghar Stanikzai    Player  809.0
3   CL White    Team    988.0
4   DA Warner   Team    1691.0

convert rows to columns in pandas dataframe


By : sam
Date : March 29 2020, 07:55 AM
wish of those help I'd like to convert to below Df1 to Df2. , Use:
code :
#create dictionary for rename columns for correct sorting
d = dict(enumerate(df.columns))
d1 = {v:k for k, v in d.items()}

#first add missing values for each `wk` and `id`
df1 = df.set_index(['wk', 'id']).unstack().stack(dropna=False).reset_index()

#for each id create DataFrame, reshape by unstask and rename columns
df1 = (df1.groupby('id')
       .apply(lambda x: pd.DataFrame(x.values, columns=df.columns))
       .unstack()
       .reset_index(drop=True)
       .rename(columns=d1, level=0)
       .sort_index(axis=1, level=1)
       .rename(columns=d, level=0))

#convert values to integers if necessary
df1.loc[:, ['wk', 'id']] = df1.loc[:, ['wk', 'id']].astype(int)

#flatten MultiIndex in columns
df1.columns = ['{}_{}'.format(a, b) for a, b in df1.columns]
print (df1)

   wk_0  id_0  col1_0  col2_0  wk_1  id_1  col1_1  col2_1  wk_2  id_2  col1_2  \
0     1     1     0.5    15.0     2     1     NaN     NaN     3     1     NaN   
1     1     2     0.5    15.0     2     2     0.5    15.0     3     2     0.5   
2     1     3     NaN     NaN     2     3     NaN     NaN     3     3     0.5   

   col2_2  
0     NaN  
1    15.0  
2    15.0  
Related Posts Related Posts :
  • String Manipulation Recursive Function
  • Filter after Groupby and Sum in pandas?
  • writing a custom function Multiply the average of x,y
  • Spotify API fetch authorization code from redirect_uri
  • sklearn use RandomizedSearchCV with custom metrics and catch Exceptions
  • IndexOutOfRange error when filling a List Python
  • sns stripplot with just top n number of categories
  • Python classes keep calling eachother
  • How do I create a Dataframe_new in python from an existing Dataframe_old.
  • calculating an intercept point between a straight line and an ellipse - python
  • Integrating Tensorflow object detection with keras cnn classifier
  • How to skip comma while reading CSV file in python?
  • Stop Integrating when Output Reaches 0 in scipy.integrate.odeint
  • Changing the current graph of tf.placeholder objects in Tensorflow: Is it possible?
  • Logical error in while statement when used with or operator
  • django-rest-framework: int() argument must be a string, a bytes-like object or a number, not Deferred Attribute
  • how to remove a whitespace in a list in python?
  • How to reduce the number of row repetitions in a numpy array
  • Python: Dividing values of nested list with values with values of dictionary
  • Printing empty Pyramid
  • Python: How to save log file toSharePoint
  • Python Pandas count most frequent occurrences
  • How can I store / cache values from methods in a class for later use in other methods of the same class?
  • Sklearn: Pass class names to make_scorer
  • PyTorch - applying attention efficiently
  • How do I capitalize each parameter in a function definition using Python?
  • Regex matching of a bytes pattern gives unusual results - '.' not equivalent to [\x00-\xff]
  • I need help converting this REST API Curl command to Python requests
  • How do you make a variable comparison to decide a better score in a dice game?
  • How do I run sumo-gui on instant-veins-4.7.1-i1.ova
  • Deal with NAN values when creating models with python
  • Python requests: having a space in header for posting
  • Adding a column to a pandas dataframe based on cell values
  • Get mongod rs.status() results from a python script
  • ImportError: C extension: No module named 'parsing' not built
  • python pandas update column values related to previous updated row during iteration over it
  • 3 nested loops: Optimizing a simple simulation for speed
  • Assign subset of values to pandas dataframe with MultiIndex
  • How to group two sets of buttons on each top corner of the screen using Tkinter?
  • django login using class based for custom user
  • MRJob sort reducer output
  • Python Pandas Counts using rolling time window
  • Getting or editing a string from a column in a csv file with pandas
  • Python - Delete row in matrix/array if row contains
  • Using dicom Images with OpenCV in Python
  • Odoo ghost record
  • Creating and assigning multiple variables in a tkinter application
  • Graph dictionary
  • No changes to original dataframe after applying loop
  • AUC of Random forest model is lower after tuning parameters using hypergrid search and CV with 10 folds
  • Python: Reading multiple CSV files, and assigning each to a different variable
  • How to identify empty rectangle using OpenCV
  • How to iterate multilevel dataframe in python
  • How to limit the contour plot with a line plot?
  • Why subclassing a str or int behaves differently from subclising a list or dict?
  • Python decode with translation table
  • i need to click unordered links in the below URL using selenium, python
  • How to join pandas dataframe with itself?
  • How to apply a color cast to a video frame in OpenCV Python?
  • Is there any existing library for median filtering with kernel size greater then 5 using OpenCL acceleration in python?
  • shadow
    Privacy Policy - Terms - Contact Us © voile276.org