我的df有很多列,每列都有重复的值,因为它具有调查数据。例如,我的数据如下所示:
df:
Q36r9: sales platforms - Before purchasing a new car Q36r32: Advertising letters - Before purchasing a new car
Not Selected Selected
所以我想从列名中删除文本。例如,我想从第一列中获取“:”和“-”之间的文本。因此应该是这样的:“销售平台”,在第二部分中,我要转换列的值,应将“ selected”更改为列名,将“ Not Selected”更改为NaN
因此所需的输出将如下所示:
sales platforms Advertising letters
NaN Advertising letters
已编辑:如果我具有列名称,例如:
Q40r1c3: WeChat - Looking for a new car - And now if you think again - Which social media platforms or sources would you use in each situation?
如果我只想在“:”和“-”之间输入内容。它应该提取“微信”
答案 0 :(得分:2)
IIUC,
我们可以使用QT += quick
CONFIG += c++11
# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Refer to the documentation for the
# deprecated API to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += \
main.cpp
RESOURCES += qml.qrc
# Additional import path used to resolve QML modules in Qt Creator's code model
QML_IMPORT_PATH =
# Additional import path used to resolve QML modules just for Qt Quick Designer
QML_DESIGNER_IMPORT_PATH =
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
利用某些正则表达式和贪婪匹配来匹配已定义模式之间的所有内容
.*
贪婪匹配,我们可以使用import re
df.columns = [re.search(':(.*)-',i).group(1) for i in df.columns.str.strip()]
print(df.columns)
sales platforms Advertising letters
0 Not Selected None
+?
+? Quantifier — Matches between one and unlimited times, as few times as possible, expanding as needed (lazy)