Python:DF中特定部分的行到列
我在excel中有下表,如下所示:Python:DF中特定部分的行到列,python,excel,pandas,dataframe,Python,Excel,Pandas,Dataframe,我在excel中有下表,如下所示: import pandas as pd data = """ Col1 | Col2 | Col3 | Col4 Value11 | Value21 | Value31 | stuff | stuff | stuff | 2.0 stuff | stuff | stuff | 3.0 | | Total | 5.0 Value12 | Value22 | Value32
import pandas as pd
data = """ Col1 | Col2 | Col3 | Col4
Value11 | Value21 | Value31 |
stuff | stuff | stuff | 2.0
stuff | stuff | stuff | 3.0
| | Total | 5.0
Value12 | Value22 | Value32 |
stuff | stuff | stuff | 6.0
stuff | stuff | stuff | 4.0
| | Total | 10.0 """
df = pd.read_csv(pd.compat.StringIO(data), header=0, delimiter = "|")
print(df)
+---------+---------+---------+------+
| Col1 | Col2 | Col3 | Col4 |
+---------+---------+---------+------+
| Value11 | Value21 | Value31 | |
| stuff | stuff | stuff | 2.0 |
| stuff | stuff | stuff | 3.0 |
| | | Total | 5.0 |
| Value12 | Value22 | Value32 | |
| stuff | stuff | stuff | 6.0 |
| stuff | stuff | stuff | 4.0 |
| | | Total | 10.0 |
+---------+---------+---------+------+
我想这样做,以便我可以进行数据分析:
+-------+-------+--------+------+----------+---------+---------+
| Col1 | Col2 | Col3 | Col4 | Col5 | Col6 | Col7 |
+-------+-------+--------+------+----------+---------+---------+
| stuff | stuff | stuff | 2.0 | Value11 | Value21 | Value31 |
| stuff | stuff | stuff | 3.0 | Value11 | Value21 | Value31 |
| stuff | stuff | stuff | 6.0 | Value12 | Value22 | Value32 |
| stuff | stuff | stuff | 4.0 | Value12 | Value22 | Value32 |
+-------+-------+--------+------+----------+---------+---------+
也就是说,我想将每个Col1、Col2、Col3中的值转换为对应部分旁边的重复行
我看到的唯一模式是Col3中有一个“Total”变量,正好在我要转换为行的值之上
关于如何在Python中实现这一点,您有什么想法吗?您正在寻找类似的东西吗
import pandas as pd
df = pd.DataFrame(
{"Col1": ["Value11", "stuff1", "stuff1","Value12", "stuff2", "stuff2"],
"Col2": ["Value21", "stuff1", "stuff1","Value22", "stuff2", "stuff2"],
"Col3": ["Value31", "stuff1", "stuff1","Value32", "stuff2", "stuff2"],
"Col4": ["", 2, 3,"",6,4], },
index=[1, 2, 3,4,5,6])
df1 = df.loc[df['Col1'] == 'stuff1']
df2 = df.loc[df['Col1'] == 'stuff2']
dfc = pd.concat([df1,df2])
df11 = df.loc[df['Col1'] == 'Value11']
df22 = df.loc[df['Col1'] == 'Value12']
dfc1 = pd.concat([df11, df11])
dfc2 = pd.concat([df22, df22])
df_fin1 = pd.concat([dfc1, dfc2])
print(df_fin1)
dfc.reset_index(drop=True, inplace=True)
df_fin1.reset_index(drop=True, inplace=True)
df_fin = pd.concat([dfc, df_fin1], axis=1)
print(df_fin)
因此,如果只选择行并对其进行操作,则代码如下所示:
df21 = pd.concat([df.iloc[0:1], df.iloc[0:1]])
df22 = pd.concat([df.iloc[4:5], df.iloc[4:5]])
df2 = pd.concat([df21,df22])
df1 = pd.concat([df.iloc[1:3], df.iloc[5:7]])
df1.reset_index(drop=True, inplace=True)
df2.reset_index(drop=True, inplace=True)
df_f = pd.concat([df1, df2], axis=1)
谢谢你的回答,克里斯。这并不是我真正想要的,我事先不知道值11,值12是什么,也不知道“stuff”是什么,唯一一致的变量是total,它刚好高于我想要的值11,值22如果您知道数据所在的特定行,只是选择了那些行/列并对它们进行操作?问题是我不知道数据的具体位置,我只知道我想要作为列的值总是在包含一个名为“Total”的值的行之上,这是我唯一的一致模式,不幸的是,我检查了带有“Total”的行并在“总计”行上方的每一行上运行我的第二个代码段。如果您已经这样做了,请检查第二个“总计”行并执行相同的操作。这是一个循环。也许有一种更有效的方法,但那能完成工作。你知道怎么做吗?