Python3是一个csv行,在某些字段中嵌套了键值。如何将它展平成一个宽行

2024-09-29 21:37:45 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试处理一些CSV数据,通常我会在有复杂更改时使用pandas。但是,我不知道如何处理一个或多个CSV字段中的嵌套键值

所以本质上我有这样的数据

+------+------+-------------------+------+------+
| col1 | col2 | col3              | col4 | col5 |
+------+------+-------------------+------+------+
| v    | v    | ncol1=nv,ncol2=nv | v    | v    |
+------+------+-------------------+------+------+
| v    | v    | ncol3=nv          | v    | v    |
+------+------+-------------------+------+------+
| v    | v    |                   | v    | v    |
+------+------+-------------------+------+------+

我试着得到这样的东西

+------+------+-------+-------+-------+------+------+
| col1 | col2 | ncol1 | ncol2 | ncol3 | col4 | col5 |
+------+------+-------+-------+-------+------+------+
| v    | v    | nv    | nv    |       | v    | v    |
+------+------+-------+-------+-------+------+------+
| v    | v    |       |       | nv    | v    | v    |
+------+------+-------+-------+-------+------+------+
| v    | v    |       |       |       | v    | v    |
+------+------+-------+-------+-------+------+------+

Tags: csv数据pandascol2col3col1键值我会
1条回答
网友
1楼 · 发布于 2024-09-29 21:37:45

假设列C中的数据帧值是逗号分隔的字符串,代码执行以下操作

  1. 从逗号分隔的字符串创建字典
  2. 删除列C中存在的所有空值行/空行,以便可以扩展以前创建的dictionary对象
  3. 基于字典键动态创建新列
  4. 展开字典
  5. 合并空值数据帧和新创建的数据帧
import pandas as pd
import numpy as np
df=pd.DataFrame({"A":['a','b','c',],"B":['e','f','d'],"C":['D=nv,E=nv',np.nan,"D=nv"],})
#Converts string to dictionary of key-value pairs
df.loc[:,"C"]=df.loc[:,"C"].apply(lambda x: dict(map(lambda z: z.split('='),x.split(","))) if type(x)==str else np.nan)
#Drop all null values present in Column so that the dataframe can be expanded
#Separate the null and actual rows containing values into 2 separate dataframes
df_act=df.dropna(subset=["C"])
df_null=df[~df.index.isin(df_act.index)]
#Expand the Column and store in a temporary DataFrame
df_temp=df_act['C'].apply(pd.Series)
for cols in df_temp.columns:
    df_act.loc[:,cols]=np.nan
    df_null.loc[:,cols]=np.nan

#Save Contents in the actual DataFrame
df_act[df_temp.columns]=df_temp
#Drop C Column to match with Sample Output
df_act.drop("C", axis=1, inplace=True)
df_null.drop("C", axis=1, inplace=True)
#Concatenate the DataFrames
final_df=pd.concat([df_act, df_null])

请注意,删除C列只是为了使输出与提供的示例输出匹配

相关问题 更多 >

    热门问题