multi-index

Split rows according to text in two columns (Python, Pandas)

耗尽温柔 提交于 2020-01-14 00:35:00
问题 This is my dataframe (with many more letters and a length of ~35.5k) and stuff where the – are other relevant strings). All the variables are strings and ['C1','C2'] is the MultiIndex. tmp C1 C2 C3 C4 C5 Start End C8 A 1 - - - 12 14 - A 2 - - - 1,4,7 3,6,10 - A 3 - - - 16,19 17,21 - A 4 - - - 22 24 - I need it to become this (split every row that contains commas maintaining everything else): C1 C2 C3 C4 C5 Start End C8 Appearance A 1 - - - 12 14 - 1 A 2 - - - 1 3 - 1 A 2 - - - 4 6 - 2 A 2 - -

Split rows according to text in two columns (Python, Pandas)

拥有回忆 提交于 2020-01-14 00:34:16
问题 This is my dataframe (with many more letters and a length of ~35.5k) and stuff where the – are other relevant strings). All the variables are strings and ['C1','C2'] is the MultiIndex. tmp C1 C2 C3 C4 C5 Start End C8 A 1 - - - 12 14 - A 2 - - - 1,4,7 3,6,10 - A 3 - - - 16,19 17,21 - A 4 - - - 22 24 - I need it to become this (split every row that contains commas maintaining everything else): C1 C2 C3 C4 C5 Start End C8 Appearance A 1 - - - 12 14 - 1 A 2 - - - 1 3 - 1 A 2 - - - 4 6 - 2 A 2 - -

What are levels in a pandas DataFrame?

萝らか妹 提交于 2020-01-12 13:04:24
问题 I've been reading through the documentation and many explanations and examples use levels as something taken for granted. Imho the docs lack a bit on a fundamental explanation of the data structure and definitions. What are levels in a data frame? What are levels in a MultiIndex index? 回答1: I stumbled across this question while analyzing the answer to my own question, but I didn't find the John's answer satisfying enough. After a few experiments though I think I understood the levels and

What are levels in a pandas DataFrame?

倖福魔咒の 提交于 2020-01-12 13:02:59
问题 I've been reading through the documentation and many explanations and examples use levels as something taken for granted. Imho the docs lack a bit on a fundamental explanation of the data structure and definitions. What are levels in a data frame? What are levels in a MultiIndex index? 回答1: I stumbled across this question while analyzing the answer to my own question, but I didn't find the John's answer satisfying enough. After a few experiments though I think I understood the levels and

Pandas: combining header rows of a multiIndex DataFrame

梦想与她 提交于 2020-01-10 00:05:47
问题 Lets say I have a DataFrame as follows: first bar baz foo second one two one two one two three A 1 2 3 4 5 6 7 B 8 9 10 11 12 13 14 and I want to create a new DataFrame like this: barone bartwo bazone baztwo fooone footwo foothree A 1 2 3 4 5 6 7 B 8 9 10 11 12 13 14 What would be the possible code? 回答1: 1. Update using Python 3.6+ use f-string formatting with list comprehension: df.columns = [f'{i}{j}' for i, j in df.columns] 2. Use map and join : df.columns = df.columns.map(''.join) 3. If

Pandas: combining header rows of a multiIndex DataFrame

旧巷老猫 提交于 2020-01-10 00:03:27
问题 Lets say I have a DataFrame as follows: first bar baz foo second one two one two one two three A 1 2 3 4 5 6 7 B 8 9 10 11 12 13 14 and I want to create a new DataFrame like this: barone bartwo bazone baztwo fooone footwo foothree A 1 2 3 4 5 6 7 B 8 9 10 11 12 13 14 What would be the possible code? 回答1: 1. Update using Python 3.6+ use f-string formatting with list comprehension: df.columns = [f'{i}{j}' for i, j in df.columns] 2. Use map and join : df.columns = df.columns.map(''.join) 3. If

Pandas dataframe columns with different data ''rates" for hierarchical columns and rows

此生再无相见时 提交于 2020-01-07 02:54:48
问题 Consider a pandas dataframe with hierarchical columns and rows generated as follows: import pandas as pd import numpy as np row1 = ['a', 'b'] row2 = ['c', 'd', 'e'] row3 = ['w', 'x', 'y', 'z'] row_tuple_list = [] for r1 in row1: for r2 in row2: for r3 in row3: row_tuple_list.append((r1, r2, r3)) row_index = pd.MultiIndex.from_tuples(row_tuple_list, names=['row1', 'row2', 'row3']) col1 = ['f'] col2 = ['g', 'h'] col_tuple_list = [] for c1 in col1: for c2 in col2: col_tuple_list.append((c1, c2))

How to drop VALUES column of pivot table dataframe

我是研究僧i 提交于 2020-01-06 03:56:25
问题 Need to drop a sub-column of multi-index data frame created from pivot table Need to drop a sub-column only at specific columns(month) dynamically I have a dataframe created from pivot table and need to drop a sub-column at specific columns dynamically... if todays date is less than 15 i need to drop the sub-column Bill1 for all the months except Sep-19 ( current month ) if todays date is greater than 15 , it should drop the sub-column Bill1 for all the months except Oct-19 ( next month )

Pandas: slice one multiindex dataframe with multiindex of another when some levels don't match

可紊 提交于 2020-01-05 09:23:45
问题 I have two multiindexed dataframes, one with two levels and one with three. The first two levels match in both dataframes. I would like to find all values from the first dataframe where the first two index levels match in the second dataframe. The second data frame does not have a third level. The closest answer I have found is this: How to slice one MultiIndex DataFrame with the MultiIndex of another -- however the setup is slightly different and doesn't seem to translate to this case.

Pandas: slice one multiindex dataframe with multiindex of another when some levels don't match

空扰寡人 提交于 2020-01-05 09:23:18
问题 I have two multiindexed dataframes, one with two levels and one with three. The first two levels match in both dataframes. I would like to find all values from the first dataframe where the first two index levels match in the second dataframe. The second data frame does not have a third level. The closest answer I have found is this: How to slice one MultiIndex DataFrame with the MultiIndex of another -- however the setup is slightly different and doesn't seem to translate to this case.