Python3是一个csv行,在某些字段中嵌套了键值。如何将它展平成一个宽行
我正在尝试处理一些CSV数据,通常我会在有复杂更改时使用pandas。但是,我不知道如何处理一个或多个CSV字段中的嵌套键值 所以本质上我有这样的数据Python3是一个csv行,在某些字段中嵌套了键值。如何将它展平成一个宽行,python,pandas,csv,Python,Pandas,Csv,我正在尝试处理一些CSV数据,通常我会在有复杂更改时使用pandas。但是,我不知道如何处理一个或多个CSV字段中的嵌套键值 所以本质上我有这样的数据 +------+------+-------------------+------+------+ | col1 | col2 | col3 | col4 | col5 | +------+------+-------------------+------+------+ | v | v | ncol1=nv
+------+------+-------------------+------+------+
| 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 |
+------+------+-------+-------+-------+------+------+
假设列
C
中的数据帧值是逗号分隔的字符串,代码执行以下操作
C
中存在的所有空值行/空行,以便可以扩展先前创建的dictionary对象请注意,删除
C
列只是为了使输出与提供的样本输出匹配。检查explode
:是的,我看到了将数据放入可用数据框的低级方法,但我不知道如何实际读取数据。这看起来类似于json_normalize,但是有一个csv。你能发布csv文件吗?这个链接可能很有用:@jsmart,这个例子非常准确地显示了您的建议,以及我试图做的事情@jsmart,我没有两个很好地分离的数据结构,但是我有csv,这与我的示例中所示的完全相同。如何将嵌套字段放入一个可以使用explode的结构中。
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])