Create subcolumns in pandas dataframe python

Khan

I have a dataframe with multiple columns

df = pd.DataFrame({"cylinders":[2,2,1,1],
                  "horsepower":[120,100,89,70],
                  "weight":[5400,6200,7200,1200]})


 cylinders horsepower weight
0  2          120       5400
1  2          100       6200 
2  1           80       7200
3  1           70       1200

i would like to create a new dataframe and make two subcolumns of weight with the median and mean while gouping it by cylinders. example:

                        weight
  cylinders horsepower  median  mean
0  1          100       5299    5000
1  1          120       5100    5200
2  2           70       7200    6500
3  2           80       1200    1000

For my example tables i have used random values. I cant manage to achieve that. I know how to get median and mean its described here in this stackoverflow question. :

df.weight.median()
df.weight.mean()
df.groupby('cylinders') #groupby cylinders

But how to create this subcolumn?

DYZ

The following code fragment adds the two requested columns. It groups the rows by cylinders, calculates the mean and median of weight, and combines the original dataframe and the result:

result = df.join(df.groupby('cylinders')['weight']\
           .agg(['mean', 'median']))\
           .sort_values(['cylinders', 'mean']).ffill()
#   cylinders  horsepower  weight    mean  median
#2          1          80    7200  5800.0  5800.0
#3          1          70    1200  5800.0  5800.0
#1          2         100    6200  4200.0  4200.0
#0          2         120    5400  4200.0  4200.0

You cannot have "subcolumns" for select columns in pandas. If a column has "subcolumns," all other columns must have "subcolumns," too. It is called multiindexing.

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Python Pandas: Create DataFrame Fast

分類Dev

Python Pandas Create Dataframe using a text file

分類Dev

Python: How to create a step plot with offline plotly for a pandas DataFrame?

分類Dev

Making one dataframe out of two dataframes as separate subcolumns in pyspark

分類Dev

Filling DataFrame Pandas Python

分類Dev

Python : Pandas DataFrame to CSV

分類Dev

Use lookup values to create new pandas dataframe

分類Dev

How to create a pandas dataframe with a column as array

分類Dev

How to create a Pandas DataFrame from a list of OrderedDicts?

分類Dev

HoloViews: create boxplots for every column in a pandas dataframe

分類Dev

Python: pandas dataframe condition for slice of a dataframe

分類Dev

Calculating Percentile in Python Pandas Dataframe

分類Dev

pandas DataFrame to_sql Python

分類Dev

python pandas: nested dictionary to dataframe

分類Dev

python pandas loop append dataframe

分類Dev

Aggregating rows in python pandas dataframe

分類Dev

Expand rows in python pandas dataframe

分類Dev

Complicated aggregation of DataFrame in Python Pandas?

分類Dev

Python pandas dataframe slicing, with if condition

分類Dev

python pandas dataframe from file

分類Dev

python pandas dataframe from file

分類Dev

Improvement in pandas dataframe conversion in Python

分類Dev

how to create all zero dataframe in Python

分類Dev

Create a buffer in a dataframe based on multiple columns - Python

分類Dev

Pandas Dataframe Filter contains( '。')、Python 3.6

分類Dev

Python 3.4 : Pandas DataFrame not responding to ordered dictionary

分類Dev

python - pandas - check if date exists in dataframe

分類Dev

How to sort a Dataframe by the ocurrences in a column in Python (pandas)

分類Dev

Saving statmodels Tukey hsd into a Python pandas dataframe

Related 関連記事

  1. 1

    Python Pandas: Create DataFrame Fast

  2. 2

    Python Pandas Create Dataframe using a text file

  3. 3

    Python: How to create a step plot with offline plotly for a pandas DataFrame?

  4. 4

    Making one dataframe out of two dataframes as separate subcolumns in pyspark

  5. 5

    Filling DataFrame Pandas Python

  6. 6

    Python : Pandas DataFrame to CSV

  7. 7

    Use lookup values to create new pandas dataframe

  8. 8

    How to create a pandas dataframe with a column as array

  9. 9

    How to create a Pandas DataFrame from a list of OrderedDicts?

  10. 10

    HoloViews: create boxplots for every column in a pandas dataframe

  11. 11

    Python: pandas dataframe condition for slice of a dataframe

  12. 12

    Calculating Percentile in Python Pandas Dataframe

  13. 13

    pandas DataFrame to_sql Python

  14. 14

    python pandas: nested dictionary to dataframe

  15. 15

    python pandas loop append dataframe

  16. 16

    Aggregating rows in python pandas dataframe

  17. 17

    Expand rows in python pandas dataframe

  18. 18

    Complicated aggregation of DataFrame in Python Pandas?

  19. 19

    Python pandas dataframe slicing, with if condition

  20. 20

    python pandas dataframe from file

  21. 21

    python pandas dataframe from file

  22. 22

    Improvement in pandas dataframe conversion in Python

  23. 23

    how to create all zero dataframe in Python

  24. 24

    Create a buffer in a dataframe based on multiple columns - Python

  25. 25

    Pandas Dataframe Filter contains( '。')、Python 3.6

  26. 26

    Python 3.4 : Pandas DataFrame not responding to ordered dictionary

  27. 27

    python - pandas - check if date exists in dataframe

  28. 28

    How to sort a Dataframe by the ocurrences in a column in Python (pandas)

  29. 29

    Saving statmodels Tukey hsd into a Python pandas dataframe

ホットタグ

アーカイブ