Reputation: 83
I have pandas dataframe consist of list of text, i want to split the text by ,
df['c1']=['this is text one','this is text two','this is text three']
I tried this
new = df["c1"].str.split(",", n = 1, expand = True)
but it me Nan on new variable
expected output
c1='this is text one'
c1='this is text two'
c1='this is text three'
other output is ok as long as it split the text in the list. Thank you for your help Full code
import pandas as pd
data={"C1":[["this is text one","this is text two","this is text three"]]}
df=pd.DataFrame(data)
df.head()
Upvotes: 1
Views: 2713
Reputation: 1766
Your question is a bit confusing - you say you already have a list of texts, so why do you want to split it? If what you mean is rather that you have a dataframe with strings that mush be split by commas, you could do something like this.
import pandas as pd
df = pd.DataFrame()
df['c1']=['this is the first text, which has some commas, in it',
'this is text two, which also has commas']
df['lists'] = df['c1'].apply(lambda txt: txt.split(','))
df.head()
Running df['lists'][0]
then gives ['this is the first text', ' which has some commas', ' in it']
Upvotes: 0
Reputation: 75080
Use np.concatenate()
and call the dataframe constructor (since you already have a list of strings):
df_new=pd.DataFrame(np.concatenate(df1.C1),columns=['C1'])
#or pd.DataFrame(df1.C1.values.tolist()).T
C1
0 this is text one
1 this is text two
2 this is text three
Upvotes: 2
Reputation: 531
You don't need pandas to split an array you can use for loop
here is what you need
for i in df['C1']:
for each in i:
print(each) #outputs each element in the array
Upvotes: 2