logo
Tags down

shadow

break into multiple rows a dataset with multiple columns in a single row - pandas


By : გივი გველესიანი
Date : August 02 2020, 03:00 AM
seems to work fine You can use numpy array_split() to divide the len of the list by the number of columns you want.
code :
import pandas as pd
import numpy as np

d = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21]

pd.DataFrame(np.array_split(d,len(d)/7))
    0   1   2   3   4   5   6
0   1   2   3   4   5   6   7
1   8   9  10  11  12  13  14
2  15  16  17  18  19  20  21


Share : facebook icon twitter icon

Selecting from single dataset on multiple columns for a result with combined columns but multiple rows


By : aldrin cabusog
Date : March 29 2020, 07:55 AM
this one helps. This problem hurts my head, probably because the database design itself could be better (such as having a single Column for storing the response to any question with multiple tables to map with multiple records of the same cert id). , Try this to obtain the first result...
code :
SELECT 
CERTIFICATIONID, EMPID, LAST_UPDATED,
CASE WHEN X.QUESTION = 'Q1_STATUS' THEN 1
    WHEN X.QUESTION = 'Q2_STATUS' THEN 2
    WHEN X.QUESTION = 'Q3_STATUS' THEN 3
    WHEN X.QUESTION = 'Q4_STATUS' THEN 4
END [QUESTION],
ANSWER
FROM
( SELECT
    CERTIFICATIONID, EMPID, LAST_UPDATED, QUESTION,
    ANSWER
FROM 
   (SELECT *
   FROM CERTIFICATION_TABLE) T
UNPIVOT
   (ANSWER FOR QUESTION IN 
      (Q1_STATUS, Q2_STATUS, Q3_STATUS, Q4_STATUS)
)AS unpvt ) AS X

break down pandas dataframe column into multiple columns in a single df


By : Kyeong Jung Kim
Date : March 29 2020, 07:55 AM
I hope this helps you . I have a dataframe with index, features and time data, but the time data is in one column like this:
code :
cols = ['id','date','feature']
df = pd.DataFrame({'date': {0: 'date1', 1: 'date2', 2: 'date2'}, 
                   'id': {0: 1, 1: 2, 2: 1}, 
                  'feature': {0: 'feature1', 1: 'feature2', 2: 'feature3'}}, columns=cols)

print (df)
   id   date   feature
0   1  date1  feature1
1   2  date2  feature2
2   1  date2  feature3
df = df.groupby('id')['date','feature'] \
       .apply(lambda x: pd.DataFrame(x.values, columns=['feature','date'])) \
       .unstack() \
       .sort_index(1, level=1)

print (df)
   feature      date feature      date
         0         0       1         1
id                                    
1    date1  feature1   date2  feature3
2    date2  feature2    None      None


df.columns = ['{0[0]}_{0[1]}'.format(x)  for x in df.columns]
df = df.reset_index()
print (df)
   id feature_0    date_0 feature_1    date_1
0   1     date1  feature1     date2  feature3
1   2     date2  feature2      None      None

How to combine multiple rows into a single row with python pandas based on the values of multiple columns?


By : user1645990
Date : March 29 2020, 07:55 AM
Hope this helps I need to combine multiple rows into a single row, and the original dataframes looks like: , You can try:
code :
df_out = df.set_index(['IndividualID','DayID',df.groupby(['IndividualID','DayID']).cumcount()+1]).unstack().sort_index(level=1, axis=1)
df_out.columns = df_out.columns.map('{0[0]}_{0[1]}'.format)
df_out.reset_index()
   IndividualID  DayID  JourSequence_1  TripID_1  TripPurpose_1  \
0  200100000001      1             1.0       1.0            3.0   
1  200100000009      1             1.0      55.0            3.0   

   JourSequence_2  TripID_2  TripPurpose_2  JourSequence_3  TripID_3  \
0             2.0       2.0           31.0             3.0       3.0   
1             2.0      56.0           12.0             3.0      57.0   

   TripPurpose_3  JourSequence_4  TripID_4  TripPurpose_4  JourSequence_5  \
0           23.0             4.0       4.0            5.0             NaN   
1            4.0             4.0      58.0            6.0             5.0   

   TripID_5  TripPurpose_5  JourSequence_6  TripID_6  TripPurpose_6  
0       NaN            NaN             NaN       NaN            NaN  
1      59.0           19.0             6.0      60.0            2.0  

Multiple, multi-value columns in pandas dataset - want to make multiple rows


By : user2425022
Date : March 29 2020, 07:55 AM
To fix this issue I have this following dataset from twitter in a pandas DataFrame. , you just use different functions of dataframe:
code :
import pandas as pd

df2 = pd.DataFrame({ 'billed_charge_local_micro' : [[422040000, 422040000, 422040000]],
                 'other1': 10000,
                 'other2': 'abc'})

print(df2)

#       billed_charge_local_micro        other1   other2
# 0  [422040000, 422040000, 422040000]   10000    abc

df = df2['billed_charge_local_micro'].apply(pd.Series)

df = df.transpose()

df.columns = ["billed_charge_local_micro"]

print (df)
   billed_charge_local_micro
0  422040000
1  422040000
2  422040000

Pandas: Adding data from multiple rows into extra columns for a single row


By : user3406636
Date : March 29 2020, 07:55 AM
I wish did fix the issue. I have a pandas dataframe like this: , IUUC, you could do the following:
code :
# create array (repeat of value)
repeats = np.tile(df['value'].values, (len(df), 1))

# remove elements from the diagonal
m = repeats.shape[0]
data = repeats[~np.eye(len(df), dtype=bool)].reshape(m, -1)

# create new DataFrame
df2 = pd.DataFrame(data=data[:, :], columns='value_' + df['id'].astype(str)[1:])

# concat old and new data
result = pd.concat([df, df2], axis=1)

print(result)
   id  value  value_2  value_3
0   1     25       40       30
1   2     40       25       30
2   3     30       25       40
Related Posts Related Posts :
  • name 'df' is not defined in box plot
  • Comparing dataframe columns
  • Can I Override Global Authentication for a Single Request Type in an ApiView using DRF?
  • Celery chain performances
  • Why am I getting "asynchronous comprehension outside of an asynchronous function"?
  • Creating a file from a docker container
  • doing too many write operations in django rest framework and postgres
  • How to change the order of bar charts in Python?
  • Pandas Data Frame manipulation
  • an undefined error in a simple python code- KeyError: '284882215'
  • Pandas split column in several columns throug string replacement or regex
  • how value is passed from __init__ method in pyhton as it dose not return anyhting
  • Dynamically inherit all Python magic methods from an instance attribute
  • Asking user to input certain information
  • how to test a deep learning model in a new dataset
  • Is np.fft.fft working properly? I am getting very large frequency values
  • How can you delete similar characters at the same positions in 2 strings
  • Does insert (at the end of a list) have O(1) time complexity?
  • Automatically Creating List of Dictionaries Based Upon Two Lists of Equal Length with Python
  • Discrete Cosine Transform (DCT) Coefficient Distribution
  • multiprocessing.Pool not running on last element of iterable
  • Python: sorting string non lexicographically
  • Render images from media directory Django
  • Cannot understand why more vectorization is slower than less vectorization in this case?
  • Django - Use a property as a foreign key
  • creating a function that loops if you do not enter the correct variables
  • Confused on how to store 3D matrices in HDF5 file in matlab?
  • TOTP: Can someone use the same otp within 30s and misuse it
  • is it possible to have 2 type hints for 1 parameter in Python?
  • Can someone explain what this Numpy array property is called?
  • Better way to add the result of apply (multiple outputs) to an existing DataFrame with column names
  • Selecting choice numbers
  • Create variables from list PYTHON
  • This code takes forever to run but doesn't give an error
  • "return" and "return None" behavior difference in generator
  • AttributeError: 'str' object has no attribute 'fbind' error using kivy in Python
  • Python not importing files when not inside conda environment
  • Is it possible to override a class' __call__ method?
  • Python library for live coordinated plotting in map
  • Pandas: counting consecutive rows with condition
  • How to define that a return type of method is an implementation of superclass
  • How can I print to the Visual Studio Code console in Portuguese?
  • Google Appengine Standard Python 2.7: Can't run Google Endpoints on localhost dev_appserver.py anymore
  • google appengine Unauthorized status 401
  • Don't understand cause of this IndentationError in my tic tac toe script
  • How to read in key-value pair from a json file as a pandas dataframe?
  • Can decorator decorate a recursive function?
  • How do I create a nested for loop where I have control of the initial loop index value
  • Unexpected error when creating a SQLite database using python
  • Pythonic way to write cascading of loops and if statements?
  • Python Beginner - Having trouble with multiple choice quiz program
  • Itertools return value NOT used in combinations
  • Return a list of words that contain a letter
  • From rows to columns using Peewee ORM
  • Parse large text document, to keep only "account number", and a specific keyword ("Market Value")
  • Cannot append to my list without getting a nonetype object error
  • Python Train Test Split
  • Optimizing following Python List of Dictionary operation with better solution
  • In Pandas merge colum1 value with colum2, both col data type is object and only few values are null in first column?
  • Python run multiple background loops independently
  • shadow
    Privacy Policy - Terms - Contact Us © voile276.org