seperate row made of numbers to a new column [duplicate] - python

This question already has answers here:
How to extract every second row into seperate column in dataframe?
(5 answers)
Closed 1 year ago.
I have the following data in pandas from a .CSV file:
ABC
123
DEF
456
GHI
789
But I want to separate the row made of numbers to a new column like:
ABC 123
DEF 456
GHI 789
Any idea how I can do this in pandas?
thank you.

df = pd.DataFrame(df['YOUR_COLUMN'].values.reshape(-1, 2), columns=['Letters', 'Numbers'])

Related

How to display rows where is more than 2 values in column in Python Pandas? [duplicate]

This question already has answers here:
Use word count in Pandas dataframe to drop rows with only one word
(4 answers)
Closed 1 year ago.
I have DataFrame in Python Pandas like below:
col1
-------
John One
John Kole Ole
Mike Robe Gut
Michael Spark
How can I display only these values from column in above DataFrame, which have more than 2 values, so or example to display only John Kole Ole and Mike Robe Gut, because these values have more than 2 words?
How to do that in Python Pandas?
try:
mask=df['col1'].str.split(' ').str.len().gt(2)
OR
mask=df['col1'].str.count(' ').ge(2)
Finally pass that mask:
out=df[mask]
#OR
print(df[mask])

remove all the values from the column and make it blank [duplicate]

This question already has answers here:
How to add an empty column to a dataframe?
(15 answers)
Closed 2 years ago.
I have a dataframe :
a b
1 dasd
2 fsfr12341
3 %%$dasd11
4 &^hkyo1
I need to remove all the values in column b and make it a blank column
a b
1
2
3
4
Kindly help me on this.
thanks alot
Try changing the b column to empty strings '', like this:
df['b'] = ''

How to create dataframe from 2 dataframe if value exist in both dataframe [duplicate]

This question already has answers here:
Pandas Merging 101
(8 answers)
How to filter Pandas dataframe using 'in' and 'not in' like in SQL
(11 answers)
Closed 2 years ago.
I have 2 Pandas Dataframes with one column (ID).
the first one look like this:
ID
1
2
3
4
5
and the second one look like this:
ID
3
4
5
6
7
I want to make a new Dataframe by combining those 2 Dataframes, but only the value that exist on both Dataframe.
This is the result that I want:
ID
3
4
5
can you show me how to do this in the most efficient way with pandas? Thank you

How to delete an undesirable row from pandas dataframe [duplicate]

This question already has an answer here:
Deleting DataFrame row in Pandas where column value in list
(1 answer)
Closed 3 years ago.
I have pandas dataframe for exemple like :
id column1 column2
1 aaa mmm
2 bbb nnn
3 ccc ooo
4 ddd ppp
5 eee qqq
I have a list that contain some values from column1 :
[bbb],[ddd],[eee]
I need python code in order to delete from the pandas all elements existing in the list
Ps: my pandas contains 280 000 samples so I need a fast code
Thanks
You can use isin and its negation (~):
df[~df.column1.isin(['bbb','ddd', 'eee'])]
Try this:
df = df.loc[~df['B'].isin(list), :]

i have two columns in a dataframe one has column name and other has no column name how can i name them both on python pandas? [duplicate]

This question already has answers here:
assigning column names to a pandas series
(3 answers)
Closed 4 years ago.
Input
Fruit
Apple 55
Orange 43
Output
Fruit Count
Apple 55
Orange 43
I need to rename columns accordingly please help
I think need convert Series to DataFrame by:
df = df.reset_index(name='Count')

Categories