I have imported CSV file in pandas it contains, 1 column and 25000 rows, I want to transform this file into new data frame sample data:
1 column 4 rows:
ABC
122
678
abc
I want to create DataFrame with, each element of the list(ABC) as rows columns like, followed by other rows, like this :
A B C
1 2 2
6 7 8
a b c
How to perform this task, I am new to programming.
Option 1
Straightforward solution altering the view
:
pd.DataFrame(
df.values.astype(str).view('<U1'), columns=list(df.columns[0])
)
A B C
0 1 2 2
1 6 7 8
2 a b c
Note that all resultant columns are now strings. Here's how it fares performance-wise:
df = pd.concat([df] * 100000, ignore_index=True)
%timeit pd.DataFrame(
df.values.astype(str).view('<U1'), columns=list(df.columns[0])
)
122 ms ± 3.17 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
Option 2
Slower alternative using extractall
+ unstack
:
df.ABC.str.extractall('(.)')[0].unstack().rename(
columns=dict(enumerate('ABC'))
)
match A B C
0 1 2 2
1 6 7 8
2 a b c
Option 3
One more with a list comprehension:
pd.DataFrame([list(x) for x in df.ABC.astype(str)], columns=list('ABC'))
A B C
0 1 2 2
1 6 7 8
2 a b c
Another way would be
In [131]: pd.DataFrame(df.ABC.apply(list).tolist(), columns=list(df.columns[0]))
Out[131]:
A B C
0 1 2 2
1 6 7 8
2 a b c
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.