logo
Tags down

shadow

Reshape vertical series to horizontal in Python


By : Alan Jhonn Aguiar Sc
Date : October 17 2020, 08:10 PM
This might help you I have a simple series data looks like: , You can append a level to the index and unstack:
code :
counts = series.groupby(level=0).cumcount()
series.to_frame().set_index(counts, append=True).iloc[:,0].unstack()

              0        1        2
id                               
100241  Issue 1  Issue 2  Issue 3
100242  Issue 1  Issue 2  Issue 3


Share : facebook icon twitter icon

Python: Reshape 3D image series to pixel series


By : user6463474
Date : March 29 2020, 07:55 AM
I wish did fix the issue. i have a 3D numpy array of images with the shape (imageCount, width, height). , Transpose and reshape -
code :
images.transpose(1,2,0).reshape(height*width,-1)

Reshape Series into Dataframe matrix in python


By : Utsab
Date : March 29 2020, 07:55 AM
To fix this issue I have a Series with 305 entries that with Datatime index. the data looks like this , Source DF:
code :
In [159]: df
Out[159]:
                  val
date
1992-01-31   1.123077
1992-02-28  -2.174845
1992-03-31  -3.884848
1992-04-30   8.682919
1992-05-29   1.312976
1992-06-30   7.851080
1992-07-31  -3.192788
1992-08-31  -7.351976
1992-09-30  -6.782217
1992-10-30 -17.182738
1992-11-30   3.898782
1992-12-31 -26.190414
1993-01-29   2.233359
1993-02-26   6.709006
import calendar

In [158]: (df.assign(year=df.index.year, mon=df.index.month)
             .pivot(index='year', columns='mon', values='val')
             .rename(columns=dict(zip(range(13), calendar.month_name))))
Out[158]:
mon    January  February     March     April       May     June      July    August  September    October  November   December
year
1992  1.123077 -2.174845 -3.884848  8.682919  1.312976  7.85108 -3.192788 -7.351976  -6.782217 -17.182738  3.898782 -26.190414
1993  2.233359  6.709006       NaN       NaN       NaN      NaN       NaN       NaN        NaN        NaN       NaN        NaN
In [164]: pd.pivot(df.index.year, df.index.month, df['val']) \
            .rename(columns=calendar.month_name.__getitem__)
Out[164]:
date   January  February     March     April       May     June      July    August  September    October  November   December
date
1992  1.123077 -2.174845 -3.884848  8.682919  1.312976  7.85108 -3.192788 -7.351976  -6.782217 -17.182738  3.898782 -26.190414
1993  2.233359  6.709006       NaN       NaN       NaN      NaN       NaN       NaN        NaN        NaN       NaN        NaN

Python pandas: concat vertical and horizontal


By : Kabilan R
Date : March 29 2020, 07:55 AM
around this issue There are different index values, so indexes are not aligned and get NaNs:
code :
df1 = pd.DataFrame({
    'A': ['a','a','a'],
    'B': range(3)
})
print (df1)
   A  B
0  a  0
1  a  1
2  a  2

df2 = pd.DataFrame({
    'C': ['b','b','b'],
    'D': range(4,7)
}, index=[5,7,8])
print (df2)
   C  D
5  b  4
7  b  5
8  b  6
Fin = pd.concat([df1, df2], axis= 1)
print (Fin)
     A    B    C    D
0    a  0.0  NaN  NaN
1    a  1.0  NaN  NaN
2    a  2.0  NaN  NaN
5  NaN  NaN    b  4.0
7  NaN  NaN    b  5.0
8  NaN  NaN    b  6.0
Fin = pd.concat([df1.reset_index(drop=True), df2.reset_index(drop=True)], axis= 1)
print (Fin)
   A  B  C  D
0  a  0  b  4
1  a  1  b  5
2  a  2  b  6
df2.index = df1.index
Fin = pd.concat([df1, df2], axis= 1)
print (Fin)
   A  B  C  D
0  a  0  b  4
1  a  1  b  5
2  a  2  b  6

df1.index = df2.index
Fin = pd.concat([df1, df2], axis= 1)
print (Fin)
   A  B  C  D
5  a  0  b  4
7  a  1  b  5
8  a  2  b  6

Reshape dataframe from horizontal to vertical in Python


By : user3490855
Date : March 29 2020, 07:55 AM
will be helpful for those in need If there is MultiIndex in columns you can use DataFrame.stack,DataFrame.rename_axis and DataFrame.reset_index:
code :
print (df.columns)
MultiIndex([('2017', 'price'),
            ('2018', 'ratio'),
            ('2019', 'price'),
            ('2017', 'ratio'),
            ('2018', 'price'),
            ('2019', 'ratio')],
           )

df = df.stack(0).rename_axis(('city','year')).reset_index()
print (df)
   city  year  price  ratio
0    bj  2017     10   0.10
1    bj  2018     23   0.10
2    bj  2019     13   0.10
3    sh  2017     15   0.20
4    sh  2018     15   0.20
5    sh  2019     12   0.50
6    gz  2017     12   0.15
7    gz  2018     12   0.15
8    gz  2019     14   0.10
9    sz  2017     11   0.25
10   sz  2018     11   0.25
11   sz  2019     13   0.50

Array reshape issue while time series prediction with ConvLSTM in Python


By : Thomas Zunder
Date : March 29 2020, 07:55 AM
will help you Looks like an issue of pure math: You're trying to fit the dimension of size 6358 into multiple dimensions of sizes 3179 * 2 * 2 * 1 = 12716. Try setting n_seq = 1 or n_steps = 1, this way the data will have the same size when reshaping.
Related Posts Related Posts :
  • 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?
  • Changing the color of points in scatter plot for different dummy values
  • Calculate center for each polygon in a list efficiently
  • Loading modules in the same Python package
  • replacing pixels in an imagewith pixels from another image python
  • Suggestion on picking the best options of two lists (minimum and maximum )python
  • Resetting Index in a Dataframe drops the Indexed column by 1 row
  • Convert number which are str from readlines to digits - python
  • Unable to authenitcate with python minds api
  • Print variables from a query in python
  • Ipython does not see the installed library
  • Javascript-like array-method chaining in Python?
  • PyQT: Get contents CustFormWidgetIem inside QListWidgetItem
  • Bottle server: HTTPResponse vs bottle.response
  • pytorch vgg model test on one image
  • Runtime scope and `main` symbol is different inside or outside a function
  • Use anaconda in pycharm (Import libraries error, updating anaconda and virtual environment)
  • how to get the sum of a CSV column list to print
  • Python plot drop lines with repeating value in column
  • receive binary file from POST request with BaseHTTPRequestHandler
  • D-Bus - 'ServiceUnknown' exception encountered while calling a remote procedure
  • Pandas .min() method doesn't seem fastest
  • Pandas: How to reference columns of structure: ('Name', n) ('Name', n+1)
  • Read a text file and remove all characters except alphabets & spaces in Python
  • Compute all powerset intersections of two lists
  • Applying literal_eval on string of lists of POS tags gives ValueError
  • Modelling a logic puzzle
  • What is the meaning of Copy_X in sklearn linear models
  • selenium.common.exceptions.ElementNotInteractableException: Message: Element is not displayed
  • pydev debugger does not stop in breakpoint
  • Python windows path regex
  • Flask and selenium-hub are not communicating when dockerised
  • How to use groupby on a single column and perform comparisons for multiple columns in Pandas?
  • Locate a python script without absolute path
  • shadow
    Privacy Policy - Terms - Contact Us © voile276.org