Python 如何组合两个数据帧并在混合字母数字类型的列上自然排序?
我有:Python 如何组合两个数据帧并在混合字母数字类型的列上自然排序?,python,python-3.x,pandas,string,indexing,Python,Python 3.x,Pandas,String,Indexing,我有: QID问题B答案1答案2答案3 F G H I J 0 3 a 4.0 a a e g i l 1 4 b 5.0 b a r h m p 2.5 d 5.0 NaN e d b u e i z 36e5.0dhrbczi3 我想在df_1的行之间添加这一点 QID问题B答案1答案2答案3 F G H I J 2 4_1 z 5.0 bk b a r h m p 3 4_2 w 4.0 b k b c r h m p 事实上,我希望获得: QID问题B答案1答案2答案3 F G H
QID问题B答案1答案2答案3 F G H I J
0 3 a 4.0 a a e g i l
1 4 b 5.0 b a r h m p
2.5 d 5.0 NaN e d b u e i z
36e5.0dhrbczi3
我想在df_1
的行之间添加这一点
QID问题B答案1答案2答案3 F G H I J
2 4_1 z 5.0 bk b a r h m p
3 4_2 w 4.0 b k b c r h m p
事实上,我希望获得:
QID问题B答案1答案2答案3 F G H I J
0 3 a 4.0 a a e g i l
1 4 b 5.0 b a r h m p
2 4_1 z 5.0 bk b a r h m p
3 4_2 w 4.0 b k b c r h m p
4.5 d 5.0 NaN e d b u e i z
5 6 e 5.0 d h r b c z i 3
因此,我想在df1
行之后,集成第二个数据帧new_dataframe
的行,其QID由一个数字和一个子数字组成。例如,new_dataframe
中的行,其QID是4_1
,4_2
。。。应在4
之后集成
到目前为止,我尝试了以下方法:
# Now I would like to join this new dataframe with dataframe_1, respecting the index, sort it and so on.
for i, row in df1.iterrows():
qid = row['QID']
# test if there is such a QID in new_dataframe
repeated_question = new_dataframe[new_dataframe['QID'].split("_")[0] == qid]
# insert them
更新
我用实际数据尝试了特伦顿·麦金尼(Trenton McKinney)的更新答案,但得到了一个例外:
from natsort import index_natsorted as ins
import numpy as np
import pandas as pd
# read the files
df1 = pd.read_csv("/content/drive/My Drive/Auspex/QuestionBank_06082020_QGrid_and_CINT.csv", dtype={'QID': str}, low_memory=False)
df1.drop(columns=['Unnamed: 0'], inplace=True)
df2 = pd.read_csv('/content/drive/My Drive/Auspex/new_df.csv', dtype={'QID': str})
df2.drop(columns=['Unnamed: 0'], inplace=True)
# concat them
df = pd.concat([df1, df2])
# sort the values using the key parameter in sort_values
df.sort_values(by='QID', key=lambda col: np.argsort(ins(col))).reset_index(drop=True)
我得到:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-18-d9f848d1f6bf> in <module>()
14
15 # sort the values using the key parameter in sort_values
---> 16 df.sort_values(by='QID', key=lambda col: np.argsort(ins(col))).reset_index(drop=True)
TypeError: sort_values() got an unexpected keyword argument 'key'
---------------------------------------------------------------------------
TypeError回溯(最近一次调用上次)
在()
14
15#使用sort#u values中的键参数对值进行排序
--->16 df.sort_值(by='QID',key=lambda col:np.argsort(ins(col))).reset_索引(drop=True)
TypeError:sort_values()获得意外的关键字参数“key”
Updated:使用数据文件和natsort
- 对于包含字符串和数字的混合数据类型,值将不会按数字顺序排序,而是将相关值放在一起。
将与100
一起使用100_A
- 要解决此问题,请使用可用于
或pip
安装的conda
- 这将使用
的
参数对值进行排序键
来自natsort导入索引\u natsort作为ins排序
将numpy作为np导入
作为pd进口熊猫
#读文件
df1=pd.read\u csv('data/QuestionBank\u 06082020\u QGrid\u and\u CINT-QuestionBank\u 06082020\u QGrid\u and\u CINT.csv',dtype={'QID':str},内存不足=False)
df1.drop(列=['Unnamed:0'],inplace=True)
df2=pd.read_csv('data/new_df.csv',dtype={'QID':str})
df2.drop(列=['Unnamed:0'],inplace=True)
#干掉他们
df=pd.concat([df1,df2])
#使用sort_values中的键参数对值进行排序
df.sort_值(by='QID',key=lambda col:np.argsort(ins(col))).reset_索引(drop=True)
样本结果
#显示(df.iloc[:60,:1])
QID
0南
1楠
2楠
3南
4 0
50R
6 1
7 2
8 4
9 5
10 5R
11 6
12 7
13 8
14 9
15 10
16 11
17 12
18 13
19 14
20 15
21 16
22 17
23 18
24 19
25 20
26 20_1
27 20_2
28 20_3
29 20_4
30 20_5
31 20_6
32 20_7
33 21
34 22
35 23
36 24
37 24R
38 25
39 26
40 27
41 28
42 29
43 30
44 31
45 32
46 33
47 34
48 35
49 36
50 36_1
51 36_2
52 36_3
53 36_4
54 36_5
55 36_6
56 36_7
57 36_8
58 36_9
59 37
原始答案:使用测试数据
- 数据帧
可以是基于示例的df1.QID
列,而int
将是df2.QID
类型。str
- 组合数据帧时,
列需要转换为'QID'
类型,因为创建时它将是混合类型,并且不起作用。str
TypeError:“非常感谢!我尝试了它,但得到了一个TypeError异常
我以为只有当它是熊猫系列时才会触发此异常?我已经用我的答案更新了我的问题attempt@RevolucionforMonica你需要更新熊猫。密钥仅在版本1.1.0后可用sort_values()得到了一个意外的关键字参数“key”
- 组合数据帧时,