Writing to Excel CSV with a for loop using Pandas - python

I have a list in Python
numbers_list = [(2,5), (3,4), (2,6), (3,5)...]
I want to copy the list to an Excel CSV called NumberPairings but I want each combination to be in a different row and each number in the row in different columns.
So I want the excel file to look like this:
Num1 Num2
2 5
3 4
2 6
3 5
I think I should use a for loop that begins with
for item in numbers_list:
But I need help with using Pandas to write to the file in the way I want it. If you think there is an easier way than Pandas, I'm open to it as well.

You can separate the tuples into individual columns like this:
df = pd.DataFrame(data={'tuples': numbers_list})
df
tuples
0 (2, 5)
1 (3, 4)
2 (2, 6)
3 (3, 5)
df['Num1'] = df['tuples'].str[0]
df['Num2'] = df['tuples'].str[1]
df
tuples Num1 Num2
0 (2, 5) 2 5
1 (3, 4) 3 4
2 (2, 6) 2 6
3 (3, 5) 3 5
# optional create csv
df.drop(['tuples'], axis=1).to_csv(path)

Related

Splitting a float tuple to multiple columns in python dataframe

I used regex to extract patterns from a csv document, mainly the pattern is (qty x volume in L), eg: 2x2L or 3x4L. (Note that 1 cell can have more than 1 pattern, eg: I want 2x4L and 3x1L)
0 []
1 [(2, x1L), (2, x4L)]
2 [(1, x1L), (1, x4L)]
3 [(2, x4L)]
4 [(1, x4L), (1, x1L)]
...
95 [(1, x2L)]
96 [(1, x1L), (1, x4L)]
97 [(2, x1L)]
98 [(6, x1L)]
99 [(6, x1L), (4, x2L), (4, x4L)]
Name: cards__name, Length: 100, dtype: object
I want to create 3 columns called "1L" "2L" and "4L" and then for every item, take the quantity and add it to the relevant row under the relevant column.
As such
1L 2L 4L
2 0 2
1 0 1
0 0 4
1 0 1
However I am not able to index to index the tuple in order to extract the quantity and the volume size for every item.
Any ideas?
Before you will be able to use pivot you have to normalize your columns, e.g. this way:
df['multiplier_1'] = df['order_1'].apply(lambda r: r[0])
df['base_volume_1'] = df['order_1'].apply(lambda r: r[1])
In such a way you will be able to ungroup the orders and eventually split into multiple base volumes.

Pandas: how to drop rows if contains more that 2 entries?

I have a dataframe like the following
df
entry
0 (5, 4)
1 (4, 2, 1)
2 (0, 1)
3 (2, 7)
4 (9, 4, 3)
I would like to keep only the entry that contains two values
df
entry
0 (5, 4)
1 (0, 1)
2 (1, 7)
If there are tuples use Series.str.len for lengths and compare by Series.le for <= and filter in boolean indexing:
df1 = df[df['entry'].str.len().le(2)]
print (df1)
entry
0 (5, 4)
2 (0, 1)
3 (2, 7)
If there are strings compare number of , and compare by Series.lt for <:
df2 = df[df['entry'].str.count(',').lt(2)]
print (df2)
entry
0 (5,4)
2 (0,1)
3 (2,7)

Rolling over multiple columns returning one result in Pandas

Im struck over rolling a window over multiple columns in Pandas, what I have is:
df = pd.DataFrame({'A':[1,2,3,4],'B':[5,6,7,8]})
def test(ts):
print(ts.shape)
df.rolling(2).apply(test)
However the problem is that ts.shape prints (2,) and I wanted it to print (2,2), that is include the whole window of both rows and columns.
What is wrong about my intuition about how rolling works and how can I get the results im after using Pandas?
You can use a little hack - get numeric columns length by select_dtypes and use this scalar value:
df = pd.DataFrame({'A':[1,2,3,4],'B':[5,6,7,8], 'C':list('abcd')})
print (df)
A B C
0 1 5 a
1 2 6 b
2 3 7 c
3 4 8 d
cols = len(df.select_dtypes(include=[np.number]).columns)
print (cols)
2
def test(ts):
print(tuple((ts.shape[0], cols)))
return ts.sum()
(2, 2)
(2, 2)
(2, 2)
(2, 2)
(2, 2)
(2, 2)
df = df.rolling(2).apply(test)

How to run a comparison function on items in a list? [duplicate]

This question already has answers here:
How to get all possible combinations of a list’s elements?
(32 answers)
Closed 6 years ago.
I have a list of GPS coordinates. I also have a function which compares two GPS coordinates and calculates a value.
I know I can create a nested loop to run the function on every pair, but this seem inelegant.
Is there a recommended way to run a comparison function on items in a list?
Thanks.
You can use itertools.combinations:
>>> from itertools import combinations
>>> list(combinations([1,2,3,4,5],2))
[(1, 2), (1, 3), (1, 4), (1, 5), (2, 3), (2, 4), (2, 5), (3, 4), (3, 5), (4, 5)]
This is iterable, so you can iterate and process your data.
>>> for first, second in combinations([1,2,3,4,5],2):
... print first, second
... # perform your operation with first and second
1 2
1 3
1 4
1 5
2 3
2 4
2 5
3 4
3 5
4 5

How to reshape a list of tuples in Python?

I have a list of tuples that looks like this: B=[('dict1', 'dict2'), (1, 5), (2, 6), (3, 7), (4, 8)]. Of course dict1 and dict2 refer to two dictionaries which values are shown in the table-like view below.
I want to reshape it so that a table-like view is produced, with the purpose of later writing it to a csv file:
dict1 dict2
1 5
2 6
3 7
4 8
I have tried with data=B.reshape(2,5) but to no avail since this is not the way to reshape a list.
How could this be done in a pythonic way? Thanks!
Try
In [22]: import pandas as pd
In [23]: B=[('dict1', 'dict2'), (1, 5), (2, 6), (3, 7),]
In [24]: pd.DataFrame(B).to_csv("my_file.csv", header=False, index=False, sep="\t")
Result:
$ cat my_file.csv
dict1 dict2
1 5
2 6
3 7
If you want to write in a csv file try:
import csv
with open('file.csv', 'wb') as f:
writer = csv.writer(f, delimiter='\t', quoting=csv.QUOTE_NONE)
writer.writerows(B)
Result:
dict1 dict2
1 5
2 6
3 7
4 8

Categories

Resources