[英]How can I convert multiple columns in a pandas dataframe into a column containing dictionaries of those columns?
I have a very large dataframe containing the following columns:我有一个非常大的 dataframe 包含以下列:
RegAddress.CareOf,RegAddress.POBox,RegAddress.AddressLine1,RegAddress.AddressLine2,RegAddress.PostTown,RegAddress.County,RegAddress.Country,RegAddress.PostCode
I am inserting this dataframe (loaded from a CSV) into a relational database, and so would like to convert these columns into a single column, RegAddress
, containing a dictionary, which contains the keys CareOf, POBox, AddressLine1...
and so on.我正在将此 dataframe(从 CSV 加载)插入关系数据库,因此想将这些列转换为单个列
RegAddress
,其中包含一个字典,其中包含键CareOf, POBox, AddressLine1...
等等. I cannot figure out how to do this in a vectorised fashion, ie go from:我无法弄清楚如何以矢量化方式执行此操作,即 go 来自:
RegAddress.CareOf,RegAddress.POBox
Me,2
You,3
to:到:
RegAddress
{"CareOf": "Me", "POBox": 2}
{"CareOf": "You", "POBox": 3}
efficiently.有效率的。
You can use the .apply()
method to achieve this:您可以使用
.apply()
方法来实现此目的:
selected_cols = ['RegAddress.CareOf', 'RegAddress.POBox']
df2 = pd.DataFrame()
df2['RegAddress'] = df.apply(
lambda row: {
col.split('.')[1]: row[col] for col in row.index
if col in selected_cols
},
axis=1
)
Result:结果:
RegAddress
0 {'CareOf': 'Me', 'POBox': 2}
1 {'CareOf': 'You', 'POBox': 3}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.