目前有一个堆栈溢出的答案,它解释了如何为单值列重新映射Remap values in pandas column with a dict。我想知道如何将其有效地扩展到由列表组成的列。 di = {1: "A", 2: "B"}
col1 col2
0 w a
1 [1,2] 2
2 [2,2] NaN
transform to this
col1 col2
0 w a
1 [A,A] 2
2 [B,B]
我有行和列,其中的列表示实际实体。除第一列之外的列值为1或0。第一列是一个键。如果列值为1,则目标是返回列名(倒数第二列)。这是我编写的函数,它可以工作。我想知道是否有更好的方式在Pandas中表达这一点,或者甚至有更好的方式来表示这种形式的数据,使其对熊猫更友好。 def return_keys(df,productname):
df2 = df[df['Product']==productname]
print(df2)
columns = list(df2)
cust=[]
for col in columns[1:]:
我的DataFrame中有两列。如果第一列中的文本是第二列中的子字符串,我想将第一列的值替换为第二列。
示例:
Input:
col1 col2
-----------------
text1 text1 and text2
some text some other text
text 3
text 4 this is text 4
Output:
col1 col2
------------------------------
text1 and text2 text1 and text2
some
我有一个巨大的熊猫数据150000 x 330,我很好地找到列有列表[]
我试过了
df[['[]' in x for x in df.values]]
但它只返回列名。
df['FORM_SECTION.CONTRACT.FD_CONTRACT.CONTRACTING_AUTHORITY_INFORMATION.NAME_ADDRESSES_CONTACT_CONTRACT.SPECIFICATIONS_AND_ADDITIONAL_DOCUMENTS.CONTACT_DATA.E_MAILS.E_MAIL']
0 [dsfd@inven