Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.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 3.x 熊猫宽\u到\u长,列名称中带有浮动_Python 3.x_Pandas - Fatal编程技术网

Python 3.x 熊猫宽\u到\u长,列名称中带有浮动

Python 3.x 熊猫宽\u到\u长,列名称中带有浮动,python-3.x,pandas,Python 3.x,Pandas,我可以用宽到长的例子,效果很好 df = pd.DataFrame({"A1970" : {0 : "a", 1 : "b", 2 : "c"}, "A1980" : {0 : "d", 1 : "e", 2 : "f"}, "B1970" : {0 : 2.5, 1 : 1.2, 2 : .7}, "B1980" : {0 : 3.2, 1 : 1.3, 2 : .1},

我可以用宽到长的例子,效果很好

df = pd.DataFrame({"A1970" : {0 : "a", 1 : "b", 2 : "c"}, 
                   "A1980" : {0 : "d", 1 : "e", 2 : "f"},
                   "B1970" : {0 : 2.5, 1 : 1.2, 2 : .7},
                   "B1980" : {0 : 3.2, 1 : 1.3, 2 : .1},
                   "X"     : dict(zip(range(3), np.random.randn(3)))})
df["id"] = df.index

df
   A1970 A1980  B1970  B1980         X  id
 0     a     d    2.5    3.2 -1.085631   0
 1     b     e    1.2    1.3  0.997345   1
 2     c     f    0.7    0.1  0.282978   2

pd.wide_to_long(df, ["A", "B"], i="id", j="year")

   id year
 0  1970 -1.085631  a  2.5
 1  1970  0.997345  b  1.2
 2  1970  0.282978  c  0.7
 0  1980 -1.085631  d  3.2
 1  1980  0.997345  e  1.3
 2  1980  0.282978  f  0.1
现在,我们将修改带有浮点数的列名称,如下所示:

df = pd.DataFrame({"A19.70" : {0 : "a", 1 : "b", 2 : "c"}, 
                   "A19.80" : {0 : "d", 1 : "e", 2 : "f"},
                   "B19.70" : {0 : 2.5, 1 : 1.2, 2 : .7},
                   "B19.80" : {0 : 3.2, 1 : 1.3, 2 : .1},
                   "X"     : dict(zip(range(3), np.random.randn(3)))})
df["id"] = df.index

df
   A19.70 A19.80  B19.70  B19.80         X  id
 0     a     d    2.5    3.2 -1.085631   0
 1     b     e    1.2    1.3  0.997345   1
 2     c     f    0.7    0.1  0.282978   2
我有一个空数据框,其中包含以下代码:

pd.wide_to_long(df, ["A", "B"], i="id", j="year")

检索此结果的任何想法:

       id year
 0  19.70 -1.085631  a  2.5
 1  19.70  0.997345  b  1.2
 2  19.70  0.282978  c  0.7
 0  19.80 -1.085631  d  3.2
 1  19.80  0.997345  e  1.3
 2  19.80  0.282978  f  0.1

感谢您的帮助

后缀的默认捕获组是
'\d+'
,它完成了预期的功能,但文档存在误导性/措词错误:

“\d+”捕获数字后缀

“(\d+”
不是十进制数的正确捕获组,只能捕获整数后缀

因此,您需要手动指定后缀捕获组。或者指定要将任何内容作为后缀,不使用任何内容作为分隔符。或者,对于可能只有一个小数的数字,使用类似于
后缀='[0-9]+\.?([0-9]+)?'
的方法可能更安全,例如(19,19,19.1231):

       id year
 0  19.70 -1.085631  a  2.5
 1  19.70  0.997345  b  1.2
 2  19.70  0.282978  c  0.7
 0  19.80 -1.085631  d  3.2
 1  19.80  0.997345  e  1.3
 2  19.80  0.282978  f  0.1
import pandas as pd

pd.wide_to_long(df, ["A", "B"], i="id", j="year", sep='', suffix='.*')

                X  A    B
id year                  
0  19.7 -1.182495  a  2.5
1  19.7  1.126017  b  1.2
2  19.7  0.871408  c  0.7
0  19.8 -1.182495  d  3.2
1  19.8  1.126017  e  1.3
2  19.8  0.871408  f  0.1