您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

Python – 转换数据帧和切片

5b51 2022/1/14 8:21:30 python 字数 2979 阅读 489 来源 www.jb51.cc/python

我附上了截图以帮助解释.我有一个从克利夫兰心脏数据集中提取的数据框,该数据集占用76列并将它们放入7列,并将其他列包装到下一行.我试图弄清楚如何将该数据帧变为可读格式,如右侧的数据框所示.变量xyz将始终相同,但我列出的其他字母变量将不同.我以为我可以使用data.loc [:,:'xyz']开始,但我不知道从哪里开始:data = pd.read_csv(

概述

我附上了截图以帮助解释.我有一个从克利夫兰心脏数据集中提取的数据框,该数据集占用76列并将它们放入7列,并将其他列包装到下一行.我试图弄清楚如何将该数据帧变为可读格式,如右侧的数据框所示.

enter image description here

变量xyz将始终相同,但我列出的其他字母变量将不同.我以为我可以使用data.loc [:,:’xyz’]开始,但我不知道从哪里开始:

data = pd.read_csv("../resources/cleveland.data")
data.loc[:,:'xyz']

然后我必须从那里开始为这些变量分配列名.令人惊讶的是,一旦我解决了这个问题,火车,测试,验证部分将更加容易.在此先感谢您的帮助. (我是菜鸟)

1   a   b   c
d   xyz 2   e
f   g   h   xyz
3   i   j   k

import pandas as pd
import numpy as np

# The initial data doesn't contain header so set header to None
df = pd.read_csv("../resources/cleveland.data",header=None)
cols = df.columns.tolist()

# Reset the index to get the line number in the durty file
df = df.reset_index()

# After having melt the df,you can filter the df in order to have every values in one column.
# Those values are in the right order
df = pd.melt(df,id_vars=['index'],value_vars=cols)
df = df.sort_values(by=['index','variable'])

# Then you can set the line number
df['line'] = np.where(df.value == 'xyz',1,np.nan)
df.line = df.line.cumsum()
df.line = df.line.bfill()

# If the file doesn't end with 'xyz',we have to set the line number to df.line.max() + 1
df.loc[df.line.isna(),'line'] = df.line.max() + 1
df.line = df.line.ffill()

# We can set the column names as interger with a groupby cumsum
df['one'] = 1
df['col_name'] = df.groupby(['line'])['one'].cumsum()
df['col_name'] = "col_" + df['col_name'].astype('str')

# Then we can pivot the table
df = df[['value','line','col_name']]
df = df.pivot(index='line',columns='col_name',values='value')
print(df)

输出数据

col_name col_1 col_2 col_3 col_4 col_5 col_6
line
1.0          1     a     b     c     d   xyz
2.0          2     e     f     g     h   xyz
3.0          3     i     j     k   NaN   NaN

总结

以上是编程之家为你收集整理的Python – 转换数据帧和切片全部内容,希望文章能够帮你解决Python – 转换数据帧和切片所遇到的程序开发问题。


如果您也喜欢它,动动您的小指点个赞吧

除非注明,文章均由 laddyq.com 整理发布,欢迎转载。

转载请注明:
链接:http://laddyq.com
来源:laddyq.com
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。


联系我
置顶