Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 动态分解行中的列_Python_Python 3.x_Python 2.7_Pandas_Dataframe - Fatal编程技术网

Python 动态分解行中的列

Python 动态分解行中的列,python,python-3.x,python-2.7,pandas,dataframe,Python,Python 3.x,Python 2.7,Pandas,Dataframe,我有一个DataFrame,每行有多个列,每列需要拆分成新行 当前(缩短)的数据帧如下所示 |---------------------------------------------------------------------------------------------------------------------------------| |institution_short_name |interest_paid1 |interest_paid2 |product_detail_

我有一个
DataFrame
,每行有多个列,每列需要拆分成新行

当前(缩短)的
数据帧如下所示

|---------------------------------------------------------------------------------------------------------------------------------|
|institution_short_name |interest_paid1 |interest_paid2 |product_detail_value_min |term_01m_value |term_02y_value |term_03m_value |
|---------------------------------------------------------------------------------------------------------------------------------|
|One                    |Z              |Q              |2000                     |0.50           |0.75           |0.75           |
|One                    |Z              |Q              |5000                     |0.50           |3.65           |3.75           |
|One                    |M              |M              |20000                    |Nan            |3.65           |Nan            | 
|---------------------------------------------------------------------------------------------------------------------------------|
对于每个
术语
列,例如,我想从列的名称以及该列下该行的值中提取01m、02y、03m,并按如下方式分解它:

|----------------------------------------------------------------------------------------------------------|
|institution_short_name |interest_paid1 |interest_paid2 |product_detail_value_min |Term            |Value  |
|----------------------------------------------------------------------------------------------------------|
|One                    |Z              |Q              |2000                     |01m             |0.5    |
|One                    |Z              |Q              |2000                     |02y             |3.75   |
|One                    |Z              |Q              |2000                     |03m             |0.75   |
|One                    |Z              |Q              |5000                     |01m             |0.5    |
|One                    |Z              |Q              |5000                     |02y             |3.65   |
|One                    |Z              |Q              |5000                     |03m             |3.75   |
|One                    |M              |M              |20000                    |02y             |3.65   |
|----------------------------------------------------------------------------------------------------------|
我不是要一个完整的解决方案,只是想让我开始。谢谢

您可以查看

你可以查一下


熊猫中的
explode
是什么?@Prateek:,它将“类似列表”(例如对象列表、字符串列表)拆分为多行。熊猫中的
explode
是什么?@Prateek:,它将“类似列表”(例如对象列表、字符串列表)拆分为多行。谢谢,这正是我正在查找的内容for@PhilBainesyw:-),一个提示
suffix='\\w+'
这是最令人困惑的部分,我以前对它有问题:-)@PhilBaines如果这是您需要的,您介意接受吗?谢谢,这正是我要找的for@PhilBainesyw:-),一个提示
后缀='\\w+'
这是最容易混淆的部分,我以前对它有意见:-)@PhilBaines如果这是你需要的,你介意接受吗?
pd.wide_to_long(df.repalce('Nan',np.nan),['term'],i=['institution_short_name','interest_paid1','interest_paid2','product_detail_value_min'],j='Term',suffix='\\w+').reset_index().dropna()