[英]If cell contains more than one string, put in to the new cell in Pandas
So I'm working with Pandas and I have multiple words (ie strings) in one cell, and I need to put every word into the new row and keep coordinated data. 所以我正在与Pandas一起工作,并且在一个单元格中有多个单词(即字符串),因此我需要将每个单词放到新行中并保持协调的数据。 I've found a method which could help me,but it works with numbers, not strings.
我找到了一种可以帮助我的方法,但是它适用于数字,而不是字符串。 So what method do I need to use?
那我需要用什么方法呢?
Simple example of my table: 我的表的简单示例:
id name method
1 adenosis mammography, mri
And I need it to be: 我需要它是:
id name method
1 adenosis mammography
mri
Thanks! 谢谢!
UPDATE: 更新:
That's what I'm trying to do, according to @jezrael's proposal: 根据@jezrael的建议,这就是我想要做的:
import pandas as pd
import numpy as np
xl = pd.ExcelFile("./dev/eyetoai/google_form_pure.xlsx")
xl.sheet_names
df = xl.parse("Form Responses 1")
df.groupby(['Name of condition','Condition description','Relevant Modality','Type of finding Mammography', 'Type of finding MRI', 'Type of finding US']).mean()
splitted = df['Relevant Modality'].str.split(',')
l = splitted.str.len()
df = pd.DataFrame({col: np.repeat(df[col], l) for col in ['Name of condition','Condition description']})
df['Relevant Modality'] = np.concatenate(splitted)
But I have this type of error: TypeError: repeat() takes exactly 2 arguments (3 given) 但是我有这种类型的错误:TypeError:repeat()正好接受2个参数(给定3个)
You can use read_excel
+ split
+ stack
+ drop
+ join
+ reset_index
: 您可以使用
read_excel
+ split
+ stack
+ drop
+ join
+ reset_index
:
#define columns which need split by , and then flatten them
cols = ['Condition description','Relevant Modality']
#read csv to dataframe
df = pd.read_excel('Untitled 1.xlsx')
#print (df)
df1 = pd.DataFrame({col: df[col].str.split(',', expand=True).stack() for col in cols})
print (df1)
Condition description Relevant Modality
0 0 Fibroadenomas are the most common cause of a b... Mammography
1 NaN US
2 NaN MRI
1 0 Papillomas are benign neoplasms Mammography
1 arising in a duct US
2 either centrally or peripherally within the b... MRI
3 leading to a nipple discharge. As they are of... NaN
4 the discharge may be bloodstained. NaN
2 0 OK Mammography
3 0 breast cancer Mammography
1 NaN US
4 0 breast inflammation Mammography
1 NaN US
#remove original columns
df = df.drop(cols, axis=1)
#create Multiindex in original df for align rows
df.index = [df.index, [0]* len(df.index)]
#join original to flattened columns, remove Multiindex
df = df1.join(df).reset_index(drop=True)
#print (df)
The previous answer is correct, I think you should use the id of reference. 先前的答案是正确的,我认为您应该使用参考ID。 an easier way could possibly be to just parse the method string to a list:
一种更简单的方法可能只是将方法字符串解析为列表:
method_list = method.split(',')
method_list = np.asarray(method_list)
If you have any trouble with indexing when initializing your Dataframe, just set index to: 如果在初始化数据框时在索引方面遇到任何麻烦,只需将index设置为:
pd.Dataframe(data, index=[0,0])
df.set_index('id')
passing the list as a value for your method key will automatically create a copy of both the index - 'id' and 'name' 将列表作为方法键的值传递时,将自动创建索引的副本-'id'和'name'
id method name
1 mammography adenosis
1 mri adenosis
I hope this helps, all the best 我希望这对你有帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.